blob: 05b2b46f18a0df54383a1ed501b2c2cabf28d8b4 [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ARM.h"
Bill Wendling92b5a2e2010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Evan Cheng75972122011-01-13 07:58:56 +000012#include "ARMMCExpr.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000013#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000014#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
17#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000018#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000019#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000022#include "llvm/Target/TargetRegistry.h"
23#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000027#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000028#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000030using namespace llvm;
31
Chris Lattner3a697562010-10-28 17:20:03 +000032namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000033
34class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000035
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000036class ARMAsmParser : public TargetAsmParser {
37 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000038 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000039
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000040 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000041 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
42
43 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000044 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
45
Chris Lattnere5658fa2010-10-30 04:09:10 +000046 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000047 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000048 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Anderson00828302011-03-18 22:50:18 +000049 bool TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000050 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000051 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
52 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000053 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000054 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000055 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
56 MCSymbolRefExpr::VariantKind Variant);
57
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000058
Kevin Enderby9c41fa82009-10-30 22:55:57 +000059 bool ParseMemoryOffsetReg(bool &Negative,
60 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000061 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000062 const MCExpr *&ShiftAmount,
63 const MCExpr *&Offset,
64 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000065 int &OffsetRegNum,
66 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000067 bool ParseShift(enum ARM_AM::ShiftOpc &St,
68 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000069 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000070 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000072 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000073 bool ParseDirectiveSyntax(SMLoc L);
74
Chris Lattner7036f8b2010-09-29 01:42:58 +000075 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000076 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000077 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000078 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
79 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000080
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000081 /// @name Auto-generated Match Functions
82 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000083
Chris Lattner0692ee62010-09-06 19:11:01 +000084#define GET_ASSEMBLER_HEADER
85#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000086
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000087 /// }
88
Jim Grosbachf922c472011-02-12 01:34:40 +000089 OperandMatchResultTy tryParseCoprocNumOperand(
90 SmallVectorImpl<MCParsedAsmOperand*>&);
91 OperandMatchResultTy tryParseCoprocRegOperand(
92 SmallVectorImpl<MCParsedAsmOperand*>&);
93 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000094 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +000095 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000096 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +000097 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000098 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000099 OperandMatchResultTy tryParseMemMode2Operand(
100 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000101 OperandMatchResultTy tryParseMemMode3Operand(
102 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000103
104 // Asm Match Converter Methods
105 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
106 const SmallVectorImpl<MCParsedAsmOperand*> &);
107 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
108 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000109 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
110 const SmallVectorImpl<MCParsedAsmOperand*> &);
111 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
112 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000113
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000114public:
Daniel Dunbard73ada72010-07-19 00:33:49 +0000115 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +0000116 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
117 // Initialize the set of available features.
118 setAvailableFeatures(ComputeAvailableFeatures(
119 &TM.getSubtarget<ARMSubtarget>()));
120 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000121
Benjamin Kramer38e59892010-07-14 22:38:02 +0000122 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000123 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000124 virtual bool ParseDirective(AsmToken DirectiveID);
125};
Jim Grosbach16c74252010-10-29 14:46:02 +0000126} // end anonymous namespace
127
Chris Lattner3a697562010-10-28 17:20:03 +0000128namespace {
129
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000130/// ARMOperand - Instances of this class represent a parsed ARM machine
131/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000132class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000133 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000134 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000135 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000136 CoprocNum,
137 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000138 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000139 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000140 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000141 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000142 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000143 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000144 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000145 DPRRegisterList,
146 SPRRegisterList,
Owen Anderson00828302011-03-18 22:50:18 +0000147 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000148 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000149 } Kind;
150
Sean Callanan76264762010-04-02 22:27:05 +0000151 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000152 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000153
154 union {
155 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000156 ARMCC::CondCodes Val;
157 } CC;
158
159 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000160 ARM_MB::MemBOpt Val;
161 } MBOpt;
162
163 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000164 unsigned Val;
165 } Cop;
166
167 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000168 ARM_PROC::IFlags Val;
169 } IFlags;
170
171 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000172 unsigned Val;
173 } MMask;
174
175 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000176 const char *Data;
177 unsigned Length;
178 } Tok;
179
180 struct {
181 unsigned RegNum;
182 } Reg;
183
Bill Wendling8155e5b2010-11-06 22:19:43 +0000184 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000185 const MCExpr *Val;
186 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000187
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000188 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000189 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000190 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000191 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000192 union {
193 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
194 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
195 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000196 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000197 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000198 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000199 unsigned Preindexed : 1;
200 unsigned Postindexed : 1;
201 unsigned OffsetIsReg : 1;
202 unsigned Negative : 1; // only used when OffsetIsReg is true
203 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000204 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000205
206 struct {
207 ARM_AM::ShiftOpc ShiftTy;
208 unsigned RegNum;
209 } Shift;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000210 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000211
Bill Wendling146018f2010-11-06 21:42:12 +0000212 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
213public:
Sean Callanan76264762010-04-02 22:27:05 +0000214 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
215 Kind = o.Kind;
216 StartLoc = o.StartLoc;
217 EndLoc = o.EndLoc;
218 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000219 case CondCode:
220 CC = o.CC;
221 break;
Sean Callanan76264762010-04-02 22:27:05 +0000222 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000223 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000224 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000225 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000226 case Register:
227 Reg = o.Reg;
228 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000229 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000230 case DPRRegisterList:
231 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000232 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000233 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000234 case CoprocNum:
235 case CoprocReg:
236 Cop = o.Cop;
237 break;
Sean Callanan76264762010-04-02 22:27:05 +0000238 case Immediate:
239 Imm = o.Imm;
240 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000241 case MemBarrierOpt:
242 MBOpt = o.MBOpt;
243 break;
Sean Callanan76264762010-04-02 22:27:05 +0000244 case Memory:
245 Mem = o.Mem;
246 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000247 case MSRMask:
248 MMask = o.MMask;
249 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000250 case ProcIFlags:
251 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000252 break;
253 case Shifter:
254 Shift = o.Shift;
255 break;
Sean Callanan76264762010-04-02 22:27:05 +0000256 }
257 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000258
Sean Callanan76264762010-04-02 22:27:05 +0000259 /// getStartLoc - Get the location of the first token of this operand.
260 SMLoc getStartLoc() const { return StartLoc; }
261 /// getEndLoc - Get the location of the last token of this operand.
262 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000263
Daniel Dunbar8462b302010-08-11 06:36:53 +0000264 ARMCC::CondCodes getCondCode() const {
265 assert(Kind == CondCode && "Invalid access!");
266 return CC.Val;
267 }
268
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000269 unsigned getCoproc() const {
270 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
271 return Cop.Val;
272 }
273
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000274 StringRef getToken() const {
275 assert(Kind == Token && "Invalid access!");
276 return StringRef(Tok.Data, Tok.Length);
277 }
278
279 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000280 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000281 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000282 }
283
Bill Wendling5fa22a12010-11-09 23:28:44 +0000284 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000285 assert((Kind == RegisterList || Kind == DPRRegisterList ||
286 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000287 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000288 }
289
Kevin Enderbycfe07242009-10-13 22:19:02 +0000290 const MCExpr *getImm() const {
291 assert(Kind == Immediate && "Invalid access!");
292 return Imm.Val;
293 }
294
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000295 ARM_MB::MemBOpt getMemBarrierOpt() const {
296 assert(Kind == MemBarrierOpt && "Invalid access!");
297 return MBOpt.Val;
298 }
299
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000300 ARM_PROC::IFlags getProcIFlags() const {
301 assert(Kind == ProcIFlags && "Invalid access!");
302 return IFlags.Val;
303 }
304
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000305 unsigned getMSRMask() const {
306 assert(Kind == MSRMask && "Invalid access!");
307 return MMask.Val;
308 }
309
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000310 /// @name Memory Operand Accessors
311 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000312 ARMII::AddrMode getMemAddrMode() const {
313 return Mem.AddrMode;
314 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000315 unsigned getMemBaseRegNum() const {
316 return Mem.BaseRegNum;
317 }
318 unsigned getMemOffsetRegNum() const {
319 assert(Mem.OffsetIsReg && "Invalid access!");
320 return Mem.Offset.RegNum;
321 }
322 const MCExpr *getMemOffset() const {
323 assert(!Mem.OffsetIsReg && "Invalid access!");
324 return Mem.Offset.Value;
325 }
326 unsigned getMemOffsetRegShifted() const {
327 assert(Mem.OffsetIsReg && "Invalid access!");
328 return Mem.OffsetRegShifted;
329 }
330 const MCExpr *getMemShiftAmount() const {
331 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
332 return Mem.ShiftAmount;
333 }
Owen Anderson00828302011-03-18 22:50:18 +0000334 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000335 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
336 return Mem.ShiftType;
337 }
338 bool getMemPreindexed() const { return Mem.Preindexed; }
339 bool getMemPostindexed() const { return Mem.Postindexed; }
340 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
341 bool getMemNegative() const { return Mem.Negative; }
342 bool getMemWriteback() const { return Mem.Writeback; }
343
344 /// @}
345
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000346 bool isCoprocNum() const { return Kind == CoprocNum; }
347 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000348 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000349 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000350 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000351 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000352 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000353 bool isDPRRegList() const { return Kind == DPRRegisterList; }
354 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000355 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000356 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000357 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000358 bool isShifter() const { return Kind == Shifter; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000359 bool isMemMode2() const {
360 if (getMemAddrMode() != ARMII::AddrMode2)
361 return false;
362
363 if (getMemOffsetIsReg())
364 return true;
365
366 if (getMemNegative() &&
367 !(getMemPostindexed() || getMemPreindexed()))
368 return false;
369
370 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
371 if (!CE) return false;
372 int64_t Value = CE->getValue();
373
374 // The offset must be in the range 0-4095 (imm12).
375 if (Value > 4095 || Value < -4095)
376 return false;
377
378 return true;
379 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000380 bool isMemMode3() const {
381 if (getMemAddrMode() != ARMII::AddrMode3)
382 return false;
383
384 if (getMemOffsetIsReg()) {
385 if (getMemOffsetRegShifted())
386 return false; // No shift with offset reg allowed
387 return true;
388 }
389
390 if (getMemNegative() &&
391 !(getMemPostindexed() || getMemPreindexed()))
392 return false;
393
394 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
395 if (!CE) return false;
396 int64_t Value = CE->getValue();
397
398 // The offset must be in the range 0-255 (imm8).
399 if (Value > 255 || Value < -255)
400 return false;
401
402 return true;
403 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000404 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000405 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
406 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000407 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000408
Daniel Dunbar4b462672011-01-18 05:55:27 +0000409 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000410 if (!CE) return false;
411
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000412 // The offset must be a multiple of 4 in the range 0-1020.
413 int64_t Value = CE->getValue();
414 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
415 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000416 bool isMemMode7() const {
417 if (!isMemory() ||
418 getMemPreindexed() ||
419 getMemPostindexed() ||
420 getMemOffsetIsReg() ||
421 getMemNegative() ||
422 getMemWriteback())
423 return false;
424
425 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
426 if (!CE) return false;
427
428 if (CE->getValue())
429 return false;
430
431 return true;
432 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000433 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000434 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000435 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000436 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000437 }
438 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000439 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000440 return false;
441
Daniel Dunbar4b462672011-01-18 05:55:27 +0000442 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000443 if (!CE) return false;
444
445 // The offset must be a multiple of 4 in the range 0-124.
446 uint64_t Value = CE->getValue();
447 return ((Value & 0x3) == 0 && Value <= 124);
448 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000449 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000450 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000451
452 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000453 // Add as immediates when possible. Null MCExpr = 0.
454 if (Expr == 0)
455 Inst.addOperand(MCOperand::CreateImm(0));
456 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000457 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
458 else
459 Inst.addOperand(MCOperand::CreateExpr(Expr));
460 }
461
Daniel Dunbar8462b302010-08-11 06:36:53 +0000462 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000463 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000464 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000465 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
466 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000467 }
468
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000469 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
470 assert(N == 1 && "Invalid number of operands!");
471 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
472 }
473
474 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
475 assert(N == 1 && "Invalid number of operands!");
476 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
477 }
478
Jim Grosbachd67641b2010-12-06 18:21:12 +0000479 void addCCOutOperands(MCInst &Inst, unsigned N) const {
480 assert(N == 1 && "Invalid number of operands!");
481 Inst.addOperand(MCOperand::CreateReg(getReg()));
482 }
483
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000484 void addRegOperands(MCInst &Inst, unsigned N) const {
485 assert(N == 1 && "Invalid number of operands!");
486 Inst.addOperand(MCOperand::CreateReg(getReg()));
487 }
488
Owen Anderson00828302011-03-18 22:50:18 +0000489 void addShifterOperands(MCInst &Inst, unsigned N) const {
490 assert(N == 1 && "Invalid number of operands!");
491 Inst.addOperand(MCOperand::CreateImm(
492 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
493 }
494
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000495 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000496 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000497 const SmallVectorImpl<unsigned> &RegList = getRegList();
498 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000499 I = RegList.begin(), E = RegList.end(); I != E; ++I)
500 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000501 }
502
Bill Wendling0f630752010-11-17 04:32:08 +0000503 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
504 addRegListOperands(Inst, N);
505 }
506
507 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
508 addRegListOperands(Inst, N);
509 }
510
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000511 void addImmOperands(MCInst &Inst, unsigned N) const {
512 assert(N == 1 && "Invalid number of operands!");
513 addExpr(Inst, getImm());
514 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000515
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000516 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
517 assert(N == 1 && "Invalid number of operands!");
518 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
519 }
520
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000521 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
522 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
523 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
524
525 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000526 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000527 assert((CE || CE->getValue() == 0) &&
528 "No offset operand support in mode 7");
529 }
530
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000531 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
532 assert(isMemMode2() && "Invalid mode or number of operands!");
533 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
534 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
535
536 if (getMemOffsetIsReg()) {
537 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
538
539 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
540 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
541 int64_t ShiftAmount = 0;
542
543 if (getMemOffsetRegShifted()) {
544 ShOpc = getMemShiftType();
545 const MCConstantExpr *CE =
546 dyn_cast<MCConstantExpr>(getMemShiftAmount());
547 ShiftAmount = CE->getValue();
548 }
549
550 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
551 ShOpc, IdxMode)));
552 return;
553 }
554
555 // Create a operand placeholder to always yield the same number of operands.
556 Inst.addOperand(MCOperand::CreateReg(0));
557
558 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
559 // the difference?
560 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
561 assert(CE && "Non-constant mode 2 offset operand!");
562 int64_t Offset = CE->getValue();
563
564 if (Offset >= 0)
565 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
566 Offset, ARM_AM::no_shift, IdxMode)));
567 else
568 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
569 -Offset, ARM_AM::no_shift, IdxMode)));
570 }
571
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000572 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
573 assert(isMemMode3() && "Invalid mode or number of operands!");
574 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
575 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
576
577 if (getMemOffsetIsReg()) {
578 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
579
580 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
581 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
582 IdxMode)));
583 return;
584 }
585
586 // Create a operand placeholder to always yield the same number of operands.
587 Inst.addOperand(MCOperand::CreateReg(0));
588
589 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
590 // the difference?
591 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
592 assert(CE && "Non-constant mode 3 offset operand!");
593 int64_t Offset = CE->getValue();
594
595 if (Offset >= 0)
596 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
597 Offset, IdxMode)));
598 else
599 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
600 -Offset, IdxMode)));
601 }
602
Chris Lattner14b93852010-10-29 00:27:31 +0000603 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
604 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000605
Daniel Dunbar4b462672011-01-18 05:55:27 +0000606 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
607 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000608
Jim Grosbach80eb2332010-10-29 17:41:25 +0000609 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
610 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000611 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000612 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000613
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000614 // The MCInst offset operand doesn't include the low two bits (like
615 // the instruction encoding).
616 int64_t Offset = CE->getValue() / 4;
617 if (Offset >= 0)
618 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
619 Offset)));
620 else
621 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
622 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000623 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000624
Bill Wendlingf4caf692010-12-14 03:36:38 +0000625 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
626 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000627 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
628 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000629 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000630
Bill Wendlingf4caf692010-12-14 03:36:38 +0000631 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
632 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000633 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
634 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000635 assert(CE && "Non-constant mode offset operand!");
636 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000637 }
638
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000639 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
640 assert(N == 1 && "Invalid number of operands!");
641 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
642 }
643
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000644 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
645 assert(N == 1 && "Invalid number of operands!");
646 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
647 }
648
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000649 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000650
Chris Lattner3a697562010-10-28 17:20:03 +0000651 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
652 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000653 Op->CC.Val = CC;
654 Op->StartLoc = S;
655 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000656 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000657 }
658
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000659 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
660 ARMOperand *Op = new ARMOperand(CoprocNum);
661 Op->Cop.Val = CopVal;
662 Op->StartLoc = S;
663 Op->EndLoc = S;
664 return Op;
665 }
666
667 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
668 ARMOperand *Op = new ARMOperand(CoprocReg);
669 Op->Cop.Val = CopVal;
670 Op->StartLoc = S;
671 Op->EndLoc = S;
672 return Op;
673 }
674
Jim Grosbachd67641b2010-12-06 18:21:12 +0000675 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
676 ARMOperand *Op = new ARMOperand(CCOut);
677 Op->Reg.RegNum = RegNum;
678 Op->StartLoc = S;
679 Op->EndLoc = S;
680 return Op;
681 }
682
Chris Lattner3a697562010-10-28 17:20:03 +0000683 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
684 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000685 Op->Tok.Data = Str.data();
686 Op->Tok.Length = Str.size();
687 Op->StartLoc = S;
688 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000689 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000690 }
691
Bill Wendling50d0f582010-11-18 23:43:05 +0000692 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000693 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000694 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000695 Op->StartLoc = S;
696 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000697 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000698 }
699
Owen Anderson00828302011-03-18 22:50:18 +0000700 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
701 SMLoc S, SMLoc E) {
702 ARMOperand *Op = new ARMOperand(Shifter);
703 Op->Shift.ShiftTy = ShTy;
704 Op->StartLoc = S;
705 Op->EndLoc = E;
706 return Op;
707 }
708
Bill Wendling7729e062010-11-09 22:44:22 +0000709 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000710 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000711 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000712 KindTy Kind = RegisterList;
713
714 if (ARM::DPRRegClass.contains(Regs.front().first))
715 Kind = DPRRegisterList;
716 else if (ARM::SPRRegClass.contains(Regs.front().first))
717 Kind = SPRRegisterList;
718
719 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000720 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000721 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000722 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000723 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000724 Op->StartLoc = StartLoc;
725 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000726 return Op;
727 }
728
Chris Lattner3a697562010-10-28 17:20:03 +0000729 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
730 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000731 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000732 Op->StartLoc = S;
733 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000734 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000735 }
736
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000737 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
738 bool OffsetIsReg, const MCExpr *Offset,
739 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000740 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000741 const MCExpr *ShiftAmount, bool Preindexed,
742 bool Postindexed, bool Negative, bool Writeback,
743 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000744 assert((OffsetRegNum == -1 || OffsetIsReg) &&
745 "OffsetRegNum must imply OffsetIsReg!");
746 assert((!OffsetRegShifted || OffsetIsReg) &&
747 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000748 assert((Offset || OffsetIsReg) &&
749 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000750 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
751 "Cannot have shift amount without shifted register offset!");
752 assert((!Offset || !OffsetIsReg) &&
753 "Cannot have expression offset and register offset!");
754
Chris Lattner3a697562010-10-28 17:20:03 +0000755 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000756 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000757 Op->Mem.BaseRegNum = BaseRegNum;
758 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000759 if (OffsetIsReg)
760 Op->Mem.Offset.RegNum = OffsetRegNum;
761 else
762 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000763 Op->Mem.OffsetRegShifted = OffsetRegShifted;
764 Op->Mem.ShiftType = ShiftType;
765 Op->Mem.ShiftAmount = ShiftAmount;
766 Op->Mem.Preindexed = Preindexed;
767 Op->Mem.Postindexed = Postindexed;
768 Op->Mem.Negative = Negative;
769 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000770
Sean Callanan76264762010-04-02 22:27:05 +0000771 Op->StartLoc = S;
772 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000773 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000774 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000775
776 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
777 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
778 Op->MBOpt.Val = Opt;
779 Op->StartLoc = S;
780 Op->EndLoc = S;
781 return Op;
782 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000783
784 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
785 ARMOperand *Op = new ARMOperand(ProcIFlags);
786 Op->IFlags.Val = IFlags;
787 Op->StartLoc = S;
788 Op->EndLoc = S;
789 return Op;
790 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000791
792 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
793 ARMOperand *Op = new ARMOperand(MSRMask);
794 Op->MMask.Val = MMask;
795 Op->StartLoc = S;
796 Op->EndLoc = S;
797 return Op;
798 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000799};
800
801} // end anonymous namespace.
802
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000803void ARMOperand::dump(raw_ostream &OS) const {
804 switch (Kind) {
805 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000806 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000807 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000808 case CCOut:
809 OS << "<ccout " << getReg() << ">";
810 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000811 case CoprocNum:
812 OS << "<coprocessor number: " << getCoproc() << ">";
813 break;
814 case CoprocReg:
815 OS << "<coprocessor register: " << getCoproc() << ">";
816 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000817 case MSRMask:
818 OS << "<mask: " << getMSRMask() << ">";
819 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000820 case Immediate:
821 getImm()->print(OS);
822 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000823 case MemBarrierOpt:
824 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
825 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000826 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000827 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000828 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
829 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000830 if (getMemOffsetIsReg()) {
831 OS << " offset:<register " << getMemOffsetRegNum();
832 if (getMemOffsetRegShifted()) {
833 OS << " offset-shift-type:" << getMemShiftType();
834 OS << " offset-shift-amount:" << *getMemShiftAmount();
835 }
836 } else {
837 OS << " offset:" << *getMemOffset();
838 }
839 if (getMemOffsetIsReg())
840 OS << " (offset-is-reg)";
841 if (getMemPreindexed())
842 OS << " (pre-indexed)";
843 if (getMemPostindexed())
844 OS << " (post-indexed)";
845 if (getMemNegative())
846 OS << " (negative)";
847 if (getMemWriteback())
848 OS << " (writeback)";
849 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000850 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000851 case ProcIFlags: {
852 OS << "<ARM_PROC::";
853 unsigned IFlags = getProcIFlags();
854 for (int i=2; i >= 0; --i)
855 if (IFlags & (1 << i))
856 OS << ARM_PROC::IFlagsToString(1 << i);
857 OS << ">";
858 break;
859 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000860 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000861 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000862 break;
Owen Anderson00828302011-03-18 22:50:18 +0000863 case Shifter:
864 OS << "<shifter " << getShiftOpcStr(Shift.ShiftTy) << ">";
865 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000866 case RegisterList:
867 case DPRRegisterList:
868 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000869 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000870
Bill Wendling5fa22a12010-11-09 23:28:44 +0000871 const SmallVectorImpl<unsigned> &RegList = getRegList();
872 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000873 I = RegList.begin(), E = RegList.end(); I != E; ) {
874 OS << *I;
875 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000876 }
877
878 OS << ">";
879 break;
880 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000881 case Token:
882 OS << "'" << getToken() << "'";
883 break;
884 }
885}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000886
887/// @name Auto-generated Match Functions
888/// {
889
890static unsigned MatchRegisterName(StringRef Name);
891
892/// }
893
Bob Wilson69df7232011-02-03 21:46:10 +0000894bool ARMAsmParser::ParseRegister(unsigned &RegNo,
895 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000896 RegNo = TryParseRegister();
897
898 return (RegNo == (unsigned)-1);
899}
900
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000901/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000902/// and if it is a register name the token is eaten and the register number is
903/// returned. Otherwise return -1.
904///
905int ARMAsmParser::TryParseRegister() {
906 const AsmToken &Tok = Parser.getTok();
907 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000908
Chris Lattnere5658fa2010-10-30 04:09:10 +0000909 // FIXME: Validate register for the current architecture; we have to do
910 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000911 std::string upperCase = Tok.getString().str();
912 std::string lowerCase = LowercaseString(upperCase);
913 unsigned RegNum = MatchRegisterName(lowerCase);
914 if (!RegNum) {
915 RegNum = StringSwitch<unsigned>(lowerCase)
916 .Case("r13", ARM::SP)
917 .Case("r14", ARM::LR)
918 .Case("r15", ARM::PC)
919 .Case("ip", ARM::R12)
920 .Default(0);
921 }
922 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000923
Chris Lattnere5658fa2010-10-30 04:09:10 +0000924 Parser.Lex(); // Eat identifier token.
925 return RegNum;
926}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000927
Owen Anderson00828302011-03-18 22:50:18 +0000928/// Try to parse a register name. The token must be an Identifier when called,
929/// and if it is a register name the token is eaten and the register number is
930/// returned. Otherwise return -1.
931///
932bool ARMAsmParser::TryParseShiftRegister(
933 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
934 SMLoc S = Parser.getTok().getLoc();
935 const AsmToken &Tok = Parser.getTok();
936 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
937
938 std::string upperCase = Tok.getString().str();
939 std::string lowerCase = LowercaseString(upperCase);
940 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
941 .Case("lsl", ARM_AM::lsl)
942 .Case("lsr", ARM_AM::lsr)
943 .Case("asr", ARM_AM::asr)
944 .Case("ror", ARM_AM::ror)
945 .Case("rrx", ARM_AM::rrx)
946 .Default(ARM_AM::no_shift);
947
948 if (ShiftTy == ARM_AM::no_shift)
949 return true;
950
951 Parser.Lex(); // Eat shift-type operand;
952 int RegNum = TryParseRegister();
953 if (RegNum == -1)
954 return Error(Parser.getTok().getLoc(), "register expected");
955
956 Operands.push_back(ARMOperand::CreateReg(RegNum,S, Parser.getTok().getLoc()));
957 Operands.push_back(ARMOperand::CreateShifter(ShiftTy,
958 S, Parser.getTok().getLoc()));
959
960 return false;
961}
962
963
Bill Wendling50d0f582010-11-18 23:43:05 +0000964/// Try to parse a register name. The token must be an Identifier when called.
965/// If it's a register, an AsmOperand is created. Another AsmOperand is created
966/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000967///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000968/// TODO this is likely to change to allow different register types and or to
969/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000970bool ARMAsmParser::
971TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000972 SMLoc S = Parser.getTok().getLoc();
973 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000974 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000975 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000976
Bill Wendling50d0f582010-11-18 23:43:05 +0000977 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000978
Chris Lattnere5658fa2010-10-30 04:09:10 +0000979 const AsmToken &ExclaimTok = Parser.getTok();
980 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000981 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
982 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000983 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000984 }
985
Bill Wendling50d0f582010-11-18 23:43:05 +0000986 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000987}
988
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000989/// MatchCoprocessorOperandName - Try to parse an coprocessor related
990/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
991/// "c5", ...
992static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000993 // Use the same layout as the tablegen'erated register name matcher. Ugly,
994 // but efficient.
995 switch (Name.size()) {
996 default: break;
997 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000998 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000999 return -1;
1000 switch (Name[1]) {
1001 default: return -1;
1002 case '0': return 0;
1003 case '1': return 1;
1004 case '2': return 2;
1005 case '3': return 3;
1006 case '4': return 4;
1007 case '5': return 5;
1008 case '6': return 6;
1009 case '7': return 7;
1010 case '8': return 8;
1011 case '9': return 9;
1012 }
1013 break;
1014 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001015 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001016 return -1;
1017 switch (Name[2]) {
1018 default: return -1;
1019 case '0': return 10;
1020 case '1': return 11;
1021 case '2': return 12;
1022 case '3': return 13;
1023 case '4': return 14;
1024 case '5': return 15;
1025 }
1026 break;
1027 }
1028
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001029 return -1;
1030}
1031
Jim Grosbachf922c472011-02-12 01:34:40 +00001032/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001033/// token must be an Identifier when called, and if it is a coprocessor
1034/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001035ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1036tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001037 SMLoc S = Parser.getTok().getLoc();
1038 const AsmToken &Tok = Parser.getTok();
1039 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1040
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001041 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001042 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001043 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001044
1045 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001046 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001047 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001048}
1049
Jim Grosbachf922c472011-02-12 01:34:40 +00001050/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001051/// token must be an Identifier when called, and if it is a coprocessor
1052/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001053ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1054tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001055 SMLoc S = Parser.getTok().getLoc();
1056 const AsmToken &Tok = Parser.getTok();
1057 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1058
1059 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1060 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001061 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001062
1063 Parser.Lex(); // Eat identifier token.
1064 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001065 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001066}
1067
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001068/// Parse a register list, return it if successful else return null. The first
1069/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001070bool ARMAsmParser::
1071ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001072 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001073 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001074 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001075
Bill Wendling7729e062010-11-09 22:44:22 +00001076 // Read the rest of the registers in the list.
1077 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001078 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001079
Bill Wendling7729e062010-11-09 22:44:22 +00001080 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001081 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001082 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001083
Sean Callanan18b83232010-01-19 21:44:56 +00001084 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001085 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001086 if (RegTok.isNot(AsmToken::Identifier)) {
1087 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001088 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001089 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001090
Bill Wendling1d6a2652010-11-06 10:40:24 +00001091 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001092 if (RegNum == -1) {
1093 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001094 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001095 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001096
Bill Wendlinge7176102010-11-06 22:36:58 +00001097 if (IsRange) {
1098 int Reg = PrevRegNum;
1099 do {
1100 ++Reg;
1101 Registers.push_back(std::make_pair(Reg, RegLoc));
1102 } while (Reg != RegNum);
1103 } else {
1104 Registers.push_back(std::make_pair(RegNum, RegLoc));
1105 }
1106
1107 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001108 } while (Parser.getTok().is(AsmToken::Comma) ||
1109 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001110
1111 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001112 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001113 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1114 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001115 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001116 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001117
Bill Wendlinge7176102010-11-06 22:36:58 +00001118 SMLoc E = RCurlyTok.getLoc();
1119 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001120
Bill Wendlinge7176102010-11-06 22:36:58 +00001121 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001122 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001123 RI = Registers.begin(), RE = Registers.end();
1124
Bill Wendling7caebff2011-01-12 21:20:59 +00001125 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001126 bool EmittedWarning = false;
1127
Bill Wendling7caebff2011-01-12 21:20:59 +00001128 DenseMap<unsigned, bool> RegMap;
1129 RegMap[HighRegNum] = true;
1130
Bill Wendlinge7176102010-11-06 22:36:58 +00001131 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001132 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001133 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001134
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001135 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001136 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001137 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001138 }
1139
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001140 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001141 Warning(RegInfo.second,
1142 "register not in ascending order in register list");
1143
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001144 RegMap[Reg] = true;
1145 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001146 }
1147
Bill Wendling50d0f582010-11-18 23:43:05 +00001148 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1149 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001150}
1151
Jim Grosbachf922c472011-02-12 01:34:40 +00001152/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1153ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1154tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001155 SMLoc S = Parser.getTok().getLoc();
1156 const AsmToken &Tok = Parser.getTok();
1157 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1158 StringRef OptStr = Tok.getString();
1159
1160 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1161 .Case("sy", ARM_MB::SY)
1162 .Case("st", ARM_MB::ST)
1163 .Case("ish", ARM_MB::ISH)
1164 .Case("ishst", ARM_MB::ISHST)
1165 .Case("nsh", ARM_MB::NSH)
1166 .Case("nshst", ARM_MB::NSHST)
1167 .Case("osh", ARM_MB::OSH)
1168 .Case("oshst", ARM_MB::OSHST)
1169 .Default(~0U);
1170
1171 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001172 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001173
1174 Parser.Lex(); // Eat identifier token.
1175 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001176 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001177}
1178
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001179/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001180ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1181tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1182 SMLoc S = Parser.getTok().getLoc();
1183 const AsmToken &Tok = Parser.getTok();
1184 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1185 StringRef IFlagsStr = Tok.getString();
1186
1187 unsigned IFlags = 0;
1188 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1189 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1190 .Case("a", ARM_PROC::A)
1191 .Case("i", ARM_PROC::I)
1192 .Case("f", ARM_PROC::F)
1193 .Default(~0U);
1194
1195 // If some specific iflag is already set, it means that some letter is
1196 // present more than once, this is not acceptable.
1197 if (Flag == ~0U || (IFlags & Flag))
1198 return MatchOperand_NoMatch;
1199
1200 IFlags |= Flag;
1201 }
1202
1203 Parser.Lex(); // Eat identifier token.
1204 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1205 return MatchOperand_Success;
1206}
1207
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001208/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1209ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1210tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1211 SMLoc S = Parser.getTok().getLoc();
1212 const AsmToken &Tok = Parser.getTok();
1213 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1214 StringRef Mask = Tok.getString();
1215
1216 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1217 size_t Start = 0, Next = Mask.find('_');
1218 StringRef Flags = "";
1219 StringRef SpecReg = Mask.slice(Start, Next);
1220 if (Next != StringRef::npos)
1221 Flags = Mask.slice(Next+1, Mask.size());
1222
1223 // FlagsVal contains the complete mask:
1224 // 3-0: Mask
1225 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1226 unsigned FlagsVal = 0;
1227
1228 if (SpecReg == "apsr") {
1229 FlagsVal = StringSwitch<unsigned>(Flags)
1230 .Case("nzcvq", 0x8) // same as CPSR_c
1231 .Case("g", 0x4) // same as CPSR_s
1232 .Case("nzcvqg", 0xc) // same as CPSR_fs
1233 .Default(~0U);
1234
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001235 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001236 if (!Flags.empty())
1237 return MatchOperand_NoMatch;
1238 else
1239 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001240 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001241 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
1242 for (int i = 0, e = Flags.size(); i != e; ++i) {
1243 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1244 .Case("c", 1)
1245 .Case("x", 2)
1246 .Case("s", 4)
1247 .Case("f", 8)
1248 .Default(~0U);
1249
1250 // If some specific flag is already set, it means that some letter is
1251 // present more than once, this is not acceptable.
1252 if (FlagsVal == ~0U || (FlagsVal & Flag))
1253 return MatchOperand_NoMatch;
1254 FlagsVal |= Flag;
1255 }
1256 } else // No match for special register.
1257 return MatchOperand_NoMatch;
1258
1259 // Special register without flags are equivalent to "fc" flags.
1260 if (!FlagsVal)
1261 FlagsVal = 0x9;
1262
1263 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1264 if (SpecReg == "spsr")
1265 FlagsVal |= 16;
1266
1267 Parser.Lex(); // Eat identifier token.
1268 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1269 return MatchOperand_Success;
1270}
1271
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001272/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1273ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1274tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001275 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001276
1277 if (ParseMemory(Operands, ARMII::AddrMode2))
1278 return MatchOperand_NoMatch;
1279
1280 return MatchOperand_Success;
1281}
1282
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001283/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1284ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1285tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1286 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1287
1288 if (ParseMemory(Operands, ARMII::AddrMode3))
1289 return MatchOperand_NoMatch;
1290
1291 return MatchOperand_Success;
1292}
1293
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001294/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1295/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1296/// when they refer multiple MIOperands inside a single one.
1297bool ARMAsmParser::
1298CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1299 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1300 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1301
1302 // Create a writeback register dummy placeholder.
1303 Inst.addOperand(MCOperand::CreateImm(0));
1304
1305 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1306 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1307 return true;
1308}
1309
1310/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1311/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1312/// when they refer multiple MIOperands inside a single one.
1313bool ARMAsmParser::
1314CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1315 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1316 // Create a writeback register dummy placeholder.
1317 Inst.addOperand(MCOperand::CreateImm(0));
1318 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1319 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1320 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1321 return true;
1322}
1323
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001324/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1325/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1326/// when they refer multiple MIOperands inside a single one.
1327bool ARMAsmParser::
1328CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1329 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1330 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1331
1332 // Create a writeback register dummy placeholder.
1333 Inst.addOperand(MCOperand::CreateImm(0));
1334
1335 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1336 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1337 return true;
1338}
1339
1340/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1341/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1342/// when they refer multiple MIOperands inside a single one.
1343bool ARMAsmParser::
1344CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1345 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1346 // Create a writeback register dummy placeholder.
1347 Inst.addOperand(MCOperand::CreateImm(0));
1348 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1349 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1350 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1351 return true;
1352}
1353
Bill Wendlinge7176102010-11-06 22:36:58 +00001354/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001355/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001356///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001357/// TODO Only preindexing and postindexing addressing are started, unindexed
1358/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001359bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001360ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1361 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001362 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001363 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001364 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001365 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001366 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001367
Sean Callanan18b83232010-01-19 21:44:56 +00001368 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001369 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1370 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001371 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001372 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001373 int BaseRegNum = TryParseRegister();
1374 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001375 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001376 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001377 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001378
Daniel Dunbar05710932011-01-18 05:34:17 +00001379 // The next token must either be a comma or a closing bracket.
1380 const AsmToken &Tok = Parser.getTok();
1381 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1382 return true;
1383
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001384 bool Preindexed = false;
1385 bool Postindexed = false;
1386 bool OffsetIsReg = false;
1387 bool Negative = false;
1388 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001389 ARMOperand *WBOp = 0;
1390 int OffsetRegNum = -1;
1391 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001392 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001393 const MCExpr *ShiftAmount = 0;
1394 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001395
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001396 // First look for preindexed address forms, that is after the "[Rn" we now
1397 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001398 if (Tok.is(AsmToken::Comma)) {
1399 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001400 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001401
Chris Lattner550276e2010-10-28 20:52:15 +00001402 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1403 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001404 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001405 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001406 if (RBracTok.isNot(AsmToken::RBrac)) {
1407 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001408 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001409 }
Sean Callanan76264762010-04-02 22:27:05 +00001410 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001411 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001412
Sean Callanan18b83232010-01-19 21:44:56 +00001413 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001414 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001415 // None of addrmode3 instruction uses "!"
1416 if (AddrMode == ARMII::AddrMode3)
1417 return true;
1418
Bill Wendling50d0f582010-11-18 23:43:05 +00001419 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1420 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001421 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001422 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001423 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1424 if (AddrMode == ARMII::AddrMode2)
1425 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001426 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001427 } else {
1428 // The "[Rn" we have so far was not followed by a comma.
1429
Jim Grosbach80eb2332010-10-29 17:41:25 +00001430 // If there's anything other than the right brace, this is a post indexing
1431 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001432 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001433 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001434
Sean Callanan18b83232010-01-19 21:44:56 +00001435 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001436
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001437 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001438 Postindexed = true;
1439 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001440
Chris Lattner550276e2010-10-28 20:52:15 +00001441 if (NextTok.isNot(AsmToken::Comma)) {
1442 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001443 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001444 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001445
Sean Callananb9a25b72010-01-19 20:27:46 +00001446 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001447
Chris Lattner550276e2010-10-28 20:52:15 +00001448 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001449 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001450 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001451 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001452 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001453 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001454
1455 // Force Offset to exist if used.
1456 if (!OffsetIsReg) {
1457 if (!Offset)
1458 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001459 } else {
1460 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1461 Error(E, "shift amount not supported");
1462 return true;
1463 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001464 }
1465
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001466 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1467 Offset, OffsetRegNum, OffsetRegShifted,
1468 ShiftType, ShiftAmount, Preindexed,
1469 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001470 if (WBOp)
1471 Operands.push_back(WBOp);
1472
1473 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001474}
1475
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001476/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1477/// we will parse the following (were +/- means that a plus or minus is
1478/// optional):
1479/// +/-Rm
1480/// +/-Rm, shift
1481/// #offset
1482/// we return false on success or an error otherwise.
1483bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001484 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001485 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001486 const MCExpr *&ShiftAmount,
1487 const MCExpr *&Offset,
1488 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001489 int &OffsetRegNum,
1490 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001491 Negative = false;
1492 OffsetRegShifted = false;
1493 OffsetIsReg = false;
1494 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001495 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001496 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001497 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001498 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001499 else if (NextTok.is(AsmToken::Minus)) {
1500 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001501 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001502 }
1503 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001504 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001505 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001506 SMLoc CurLoc = OffsetRegTok.getLoc();
1507 OffsetRegNum = TryParseRegister();
1508 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001509 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001510 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001511 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001512 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001513
Bill Wendling12f40e92010-11-06 10:51:53 +00001514 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001515 if (OffsetRegNum != -1) {
1516 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001517 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001518 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001519 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001520
Sean Callanan18b83232010-01-19 21:44:56 +00001521 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001522 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001523 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001524 OffsetRegShifted = true;
1525 }
1526 }
1527 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1528 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001529 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001530 if (HashTok.isNot(AsmToken::Hash))
1531 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001532
Sean Callananb9a25b72010-01-19 20:27:46 +00001533 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001534
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001535 if (getParser().ParseExpression(Offset))
1536 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001537 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001538 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001539 return false;
1540}
1541
1542/// ParseShift as one of these two:
1543/// ( lsl | lsr | asr | ror ) , # shift_amount
1544/// rrx
1545/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001546bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1547 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001548 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001549 if (Tok.isNot(AsmToken::Identifier))
1550 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001551 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001552 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001553 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001554 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001555 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001556 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001557 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001558 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001559 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001560 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001561 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001562 else
1563 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001564 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001565
1566 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001567 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001568 return false;
1569
1570 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001571 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001572 if (HashTok.isNot(AsmToken::Hash))
1573 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001574 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001575
1576 if (getParser().ParseExpression(ShiftAmount))
1577 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001578
1579 return false;
1580}
1581
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001582/// Parse a arm instruction operand. For now this parses the operand regardless
1583/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001584bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001585 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001586 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001587
1588 // Check if the current operand has a custom associated parser, if so, try to
1589 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001590 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1591 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001592 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001593 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1594 // there was a match, but an error occurred, in which case, just return that
1595 // the operand parsing failed.
1596 if (ResTy == MatchOperand_ParseFail)
1597 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001598
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001599 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001600 default:
1601 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001602 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001603 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001604 if (!TryParseRegisterWithWriteBack(Operands))
1605 return false;
Owen Anderson00828302011-03-18 22:50:18 +00001606 if (!TryParseShiftRegister(Operands))
1607 return false;
1608
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001609
1610 // Fall though for the Identifier case that is not a register or a
1611 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001612 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1613 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001614 // This was not a register so parse other operands that start with an
1615 // identifier (like labels) as expressions and create them as immediates.
1616 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001617 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001618 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001619 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001620 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001621 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1622 return false;
1623 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001624 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001625 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001626 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001627 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001628 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001629 // #42 -> immediate.
1630 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001631 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001632 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001633 const MCExpr *ImmVal;
1634 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001635 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001636 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001637 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1638 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001639 case AsmToken::Colon: {
1640 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001641 // FIXME: Check it's an expression prefix,
1642 // e.g. (FOO - :lower16:BAR) isn't legal.
1643 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001644 if (ParsePrefix(RefKind))
1645 return true;
1646
Evan Cheng75972122011-01-13 07:58:56 +00001647 const MCExpr *SubExprVal;
1648 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001649 return true;
1650
Evan Cheng75972122011-01-13 07:58:56 +00001651 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1652 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001653 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001654 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001655 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001656 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001657 }
1658}
1659
Evan Cheng75972122011-01-13 07:58:56 +00001660// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1661// :lower16: and :upper16:.
1662bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1663 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001664
1665 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001666 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001667 Parser.Lex(); // Eat ':'
1668
1669 if (getLexer().isNot(AsmToken::Identifier)) {
1670 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1671 return true;
1672 }
1673
1674 StringRef IDVal = Parser.getTok().getIdentifier();
1675 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001676 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001677 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001678 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001679 } else {
1680 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1681 return true;
1682 }
1683 Parser.Lex();
1684
1685 if (getLexer().isNot(AsmToken::Colon)) {
1686 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1687 return true;
1688 }
1689 Parser.Lex(); // Eat the last ':'
1690 return false;
1691}
1692
1693const MCExpr *
1694ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1695 MCSymbolRefExpr::VariantKind Variant) {
1696 // Recurse over the given expression, rebuilding it to apply the given variant
1697 // to the leftmost symbol.
1698 if (Variant == MCSymbolRefExpr::VK_None)
1699 return E;
1700
1701 switch (E->getKind()) {
1702 case MCExpr::Target:
1703 llvm_unreachable("Can't handle target expr yet");
1704 case MCExpr::Constant:
1705 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1706
1707 case MCExpr::SymbolRef: {
1708 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1709
1710 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1711 return 0;
1712
1713 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1714 }
1715
1716 case MCExpr::Unary:
1717 llvm_unreachable("Can't handle unary expressions yet");
1718
1719 case MCExpr::Binary: {
1720 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1721 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1722 const MCExpr *RHS = BE->getRHS();
1723 if (!LHS)
1724 return 0;
1725
1726 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1727 }
1728 }
1729
1730 assert(0 && "Invalid expression kind!");
1731 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001732}
1733
Daniel Dunbar352e1482011-01-11 15:59:50 +00001734/// \brief Given a mnemonic, split out possible predication code and carry
1735/// setting letters to form a canonical mnemonic and flags.
1736//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001737// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001738static StringRef SplitMnemonic(StringRef Mnemonic,
1739 unsigned &PredicationCode,
1740 bool &CarrySetting,
1741 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001742 PredicationCode = ARMCC::AL;
1743 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001744 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001745
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001746 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001747 //
1748 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001749 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1750 Mnemonic == "movs" ||
1751 Mnemonic == "svc" ||
1752 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1753 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1754 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1755 Mnemonic == "vclt" ||
1756 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1757 Mnemonic == "vcle" ||
1758 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1759 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1760 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001761 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001762
Daniel Dunbar352e1482011-01-11 15:59:50 +00001763 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001764 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001765 .Case("eq", ARMCC::EQ)
1766 .Case("ne", ARMCC::NE)
1767 .Case("hs", ARMCC::HS)
1768 .Case("lo", ARMCC::LO)
1769 .Case("mi", ARMCC::MI)
1770 .Case("pl", ARMCC::PL)
1771 .Case("vs", ARMCC::VS)
1772 .Case("vc", ARMCC::VC)
1773 .Case("hi", ARMCC::HI)
1774 .Case("ls", ARMCC::LS)
1775 .Case("ge", ARMCC::GE)
1776 .Case("lt", ARMCC::LT)
1777 .Case("gt", ARMCC::GT)
1778 .Case("le", ARMCC::LE)
1779 .Case("al", ARMCC::AL)
1780 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001781 if (CC != ~0U) {
1782 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001783 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001784 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001785
Daniel Dunbar352e1482011-01-11 15:59:50 +00001786 // Next, determine if we have a carry setting bit. We explicitly ignore all
1787 // the instructions we know end in 's'.
1788 if (Mnemonic.endswith("s") &&
1789 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1790 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1791 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1792 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1793 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1794 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1795 CarrySetting = true;
1796 }
1797
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001798 // The "cps" instruction can have a interrupt mode operand which is glued into
1799 // the mnemonic. Check if this is the case, split it and parse the imod op
1800 if (Mnemonic.startswith("cps")) {
1801 // Split out any imod code.
1802 unsigned IMod =
1803 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1804 .Case("ie", ARM_PROC::IE)
1805 .Case("id", ARM_PROC::ID)
1806 .Default(~0U);
1807 if (IMod != ~0U) {
1808 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1809 ProcessorIMod = IMod;
1810 }
1811 }
1812
Daniel Dunbar352e1482011-01-11 15:59:50 +00001813 return Mnemonic;
1814}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001815
1816/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1817/// inclusion of carry set or predication code operands.
1818//
1819// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001820void ARMAsmParser::
1821GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1822 bool &CanAcceptPredicationCode) {
1823 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1824
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001825 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1826 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1827 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1828 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1829 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1830 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1831 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1832 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1833 CanAcceptCarrySet = true;
1834 } else {
1835 CanAcceptCarrySet = false;
1836 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001837
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001838 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1839 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1840 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1841 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001842 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001843 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001844 CanAcceptPredicationCode = false;
1845 } else {
1846 CanAcceptPredicationCode = true;
1847 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001848
1849 if (isThumb)
1850 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001851 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001852 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001853}
1854
1855/// Parse an arm instruction mnemonic followed by its operands.
1856bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1857 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1858 // Create the leading tokens for the mnemonic, split by '.' characters.
1859 size_t Start = 0, Next = Name.find('.');
1860 StringRef Head = Name.slice(Start, Next);
1861
Daniel Dunbar352e1482011-01-11 15:59:50 +00001862 // Split out the predication code and carry setting flag from the mnemonic.
1863 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001864 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001865 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001866 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1867 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001868
Chris Lattner3a697562010-10-28 17:20:03 +00001869 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001870
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001871 // Next, add the CCOut and ConditionCode operands, if needed.
1872 //
1873 // For mnemonics which can ever incorporate a carry setting bit or predication
1874 // code, our matching model involves us always generating CCOut and
1875 // ConditionCode operands to match the mnemonic "as written" and then we let
1876 // the matcher deal with finding the right instruction or generating an
1877 // appropriate error.
1878 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1879 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1880
1881 // Add the carry setting operand, if necessary.
1882 //
1883 // FIXME: It would be awesome if we could somehow invent a location such that
1884 // match errors on this operand would print a nice diagnostic about how the
1885 // 's' character in the mnemonic resulted in a CCOut operand.
1886 if (CanAcceptCarrySet) {
1887 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1888 NameLoc));
1889 } else {
1890 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1891 // misspelled another mnemonic).
1892
1893 // FIXME: Issue a nice error.
1894 }
1895
1896 // Add the predication code operand, if necessary.
1897 if (CanAcceptPredicationCode) {
1898 Operands.push_back(ARMOperand::CreateCondCode(
1899 ARMCC::CondCodes(PredicationCode), NameLoc));
1900 } else {
1901 // This mnemonic can't ever accept a predication code, but the user wrote
1902 // one (or misspelled another mnemonic).
1903
1904 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001905 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001906
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001907 // Add the processor imod operand, if necessary.
1908 if (ProcessorIMod) {
1909 Operands.push_back(ARMOperand::CreateImm(
1910 MCConstantExpr::Create(ProcessorIMod, getContext()),
1911 NameLoc, NameLoc));
1912 } else {
1913 // This mnemonic can't ever accept a imod, but the user wrote
1914 // one (or misspelled another mnemonic).
1915
1916 // FIXME: Issue a nice error.
1917 }
1918
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001919 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001920 while (Next != StringRef::npos) {
1921 Start = Next;
1922 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001923 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001924
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001925 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001926 }
1927
1928 // Read the remaining operands.
1929 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001930 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001931 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001932 Parser.EatToEndOfStatement();
1933 return true;
1934 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001935
1936 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001937 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001938
1939 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001940 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001941 Parser.EatToEndOfStatement();
1942 return true;
1943 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001944 }
1945 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001946
Chris Lattnercbf8a982010-09-11 16:18:25 +00001947 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1948 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001949 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001950 }
Bill Wendling146018f2010-11-06 21:42:12 +00001951
Chris Lattner34e53142010-09-08 05:10:46 +00001952 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001953 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001954}
1955
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001956bool ARMAsmParser::
1957MatchAndEmitInstruction(SMLoc IDLoc,
1958 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1959 MCStreamer &Out) {
1960 MCInst Inst;
1961 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001962 MatchResultTy MatchResult, MatchResult2;
1963 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1964 if (MatchResult != Match_Success) {
1965 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1966 // that does not update the condition codes. So try adding a CCOut operand
1967 // with a value of reg0.
1968 if (MatchResult == Match_InvalidOperand) {
1969 Operands.insert(Operands.begin() + 1,
1970 ARMOperand::CreateCCOut(0,
1971 ((ARMOperand*)Operands[0])->getStartLoc()));
1972 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1973 if (MatchResult2 == Match_Success)
1974 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001975 else {
1976 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001977 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001978 delete CCOut;
1979 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001980 }
1981 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1982 // that updates the condition codes if it ends in 's'. So see if the
1983 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1984 // operand with a value of CPSR.
1985 else if(MatchResult == Match_MnemonicFail) {
1986 // Get the instruction mnemonic, which is the first token.
1987 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1988 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1989 // removed the 's' from the mnemonic for matching.
1990 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1991 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001992 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1993 Operands.erase(Operands.begin());
1994 delete OldMnemonic;
1995 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001996 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1997 Operands.insert(Operands.begin() + 1,
1998 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1999 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2000 if (MatchResult2 == Match_Success)
2001 MatchResult = Match_Success;
2002 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002003 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2004 Operands.erase(Operands.begin());
2005 delete OldMnemonic;
2006 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002007 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002008 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2009 Operands.erase(Operands.begin() + 1);
2010 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002011 }
2012 }
2013 }
2014 }
2015 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002016 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002017 Out.EmitInstruction(Inst);
2018 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002019 case Match_MissingFeature:
2020 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2021 return true;
2022 case Match_InvalidOperand: {
2023 SMLoc ErrorLoc = IDLoc;
2024 if (ErrorInfo != ~0U) {
2025 if (ErrorInfo >= Operands.size())
2026 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002027
Chris Lattnere73d4f82010-10-28 21:41:58 +00002028 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2029 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2030 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002031
Chris Lattnere73d4f82010-10-28 21:41:58 +00002032 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002033 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002034 case Match_MnemonicFail:
2035 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002036 case Match_ConversionFail:
2037 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002038 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002039
Eric Christopherc223e2b2010-10-29 09:26:59 +00002040 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002041 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002042}
2043
Kevin Enderby515d5092009-10-15 20:48:48 +00002044/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002045bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2046 StringRef IDVal = DirectiveID.getIdentifier();
2047 if (IDVal == ".word")
2048 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002049 else if (IDVal == ".thumb")
2050 return ParseDirectiveThumb(DirectiveID.getLoc());
2051 else if (IDVal == ".thumb_func")
2052 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2053 else if (IDVal == ".code")
2054 return ParseDirectiveCode(DirectiveID.getLoc());
2055 else if (IDVal == ".syntax")
2056 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002057 return true;
2058}
2059
2060/// ParseDirectiveWord
2061/// ::= .word [ expression (, expression)* ]
2062bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2063 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2064 for (;;) {
2065 const MCExpr *Value;
2066 if (getParser().ParseExpression(Value))
2067 return true;
2068
Chris Lattneraaec2052010-01-19 19:46:13 +00002069 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002070
2071 if (getLexer().is(AsmToken::EndOfStatement))
2072 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002073
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002074 // FIXME: Improve diagnostic.
2075 if (getLexer().isNot(AsmToken::Comma))
2076 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002077 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002078 }
2079 }
2080
Sean Callananb9a25b72010-01-19 20:27:46 +00002081 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002082 return false;
2083}
2084
Kevin Enderby515d5092009-10-15 20:48:48 +00002085/// ParseDirectiveThumb
2086/// ::= .thumb
2087bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2088 if (getLexer().isNot(AsmToken::EndOfStatement))
2089 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002090 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002091
2092 // TODO: set thumb mode
2093 // TODO: tell the MC streamer the mode
2094 // getParser().getStreamer().Emit???();
2095 return false;
2096}
2097
2098/// ParseDirectiveThumbFunc
2099/// ::= .thumbfunc symbol_name
2100bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002101 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002102 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00002103 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002104 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00002105 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00002106 if (getLexer().isNot(AsmToken::EndOfStatement))
2107 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002108 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002109
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002110 // Mark symbol as a thumb symbol.
2111 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2112 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002113 return false;
2114}
2115
2116/// ParseDirectiveSyntax
2117/// ::= .syntax unified | divided
2118bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002119 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002120 if (Tok.isNot(AsmToken::Identifier))
2121 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002122 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002123 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002124 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002125 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002126 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002127 else
2128 return Error(L, "unrecognized syntax mode in .syntax directive");
2129
2130 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002131 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002132 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002133
2134 // TODO tell the MC streamer the mode
2135 // getParser().getStreamer().Emit???();
2136 return false;
2137}
2138
2139/// ParseDirectiveCode
2140/// ::= .code 16 | 32
2141bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002142 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002143 if (Tok.isNot(AsmToken::Integer))
2144 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002145 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002146 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002147 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002148 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002149 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002150 else
2151 return Error(L, "invalid operand to .code directive");
2152
2153 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002154 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002155 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002156
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002157 // FIXME: We need to be able switch subtargets at this point so that
2158 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
2159 // includes Feature_IsThumb or not to match the right instructions. This is
2160 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
2161 if (Val == 16){
2162 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
2163 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002164 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002165 }
2166 else{
2167 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
2168 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002169 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002170 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002171
Kevin Enderby515d5092009-10-15 20:48:48 +00002172 return false;
2173}
2174
Sean Callanan90b70972010-04-07 20:29:34 +00002175extern "C" void LLVMInitializeARMAsmLexer();
2176
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002177/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002178extern "C" void LLVMInitializeARMAsmParser() {
2179 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2180 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002181 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002182}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002183
Chris Lattner0692ee62010-09-06 19:11:01 +00002184#define GET_REGISTER_MATCHER
2185#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002186#include "ARMGenAsmMatcher.inc"