blob: 29ecc182d31f2b1628c90f742560ed13dca0bba3 [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) {
Sean Callananf6d91092011-04-18 20:20:44 +0000117 MCAsmParserExtension::Initialize(_Parser);
Jim Grosbach833c93c2010-11-01 16:59:54 +0000118 // Initialize the set of available features.
119 setAvailableFeatures(ComputeAvailableFeatures(
120 &TM.getSubtarget<ARMSubtarget>()));
121 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000122
Benjamin Kramer38e59892010-07-14 22:38:02 +0000123 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000124 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000125 virtual bool ParseDirective(AsmToken DirectiveID);
126};
Jim Grosbach16c74252010-10-29 14:46:02 +0000127} // end anonymous namespace
128
Chris Lattner3a697562010-10-28 17:20:03 +0000129namespace {
130
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000131/// ARMOperand - Instances of this class represent a parsed ARM machine
132/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000133class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000134 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000135 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000136 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000137 CoprocNum,
138 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000139 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000140 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000141 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000142 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000143 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000144 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000145 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000146 DPRRegisterList,
147 SPRRegisterList,
Owen Anderson00828302011-03-18 22:50:18 +0000148 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000149 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000150 } Kind;
151
Sean Callanan76264762010-04-02 22:27:05 +0000152 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000153 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000154
155 union {
156 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000157 ARMCC::CondCodes Val;
158 } CC;
159
160 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000161 ARM_MB::MemBOpt Val;
162 } MBOpt;
163
164 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000165 unsigned Val;
166 } Cop;
167
168 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000169 ARM_PROC::IFlags Val;
170 } IFlags;
171
172 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000173 unsigned Val;
174 } MMask;
175
176 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000177 const char *Data;
178 unsigned Length;
179 } Tok;
180
181 struct {
182 unsigned RegNum;
183 } Reg;
184
Bill Wendling8155e5b2010-11-06 22:19:43 +0000185 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000186 const MCExpr *Val;
187 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000188
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000189 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000190 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000191 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000192 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000193 union {
194 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
195 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
196 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000197 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000198 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000199 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000200 unsigned Preindexed : 1;
201 unsigned Postindexed : 1;
202 unsigned OffsetIsReg : 1;
203 unsigned Negative : 1; // only used when OffsetIsReg is true
204 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000205 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000206
207 struct {
208 ARM_AM::ShiftOpc ShiftTy;
209 unsigned RegNum;
210 } Shift;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000211 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000212
Bill Wendling146018f2010-11-06 21:42:12 +0000213 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
214public:
Sean Callanan76264762010-04-02 22:27:05 +0000215 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
216 Kind = o.Kind;
217 StartLoc = o.StartLoc;
218 EndLoc = o.EndLoc;
219 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000220 case CondCode:
221 CC = o.CC;
222 break;
Sean Callanan76264762010-04-02 22:27:05 +0000223 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000224 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000225 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000226 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000227 case Register:
228 Reg = o.Reg;
229 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000230 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000231 case DPRRegisterList:
232 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000233 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000234 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000235 case CoprocNum:
236 case CoprocReg:
237 Cop = o.Cop;
238 break;
Sean Callanan76264762010-04-02 22:27:05 +0000239 case Immediate:
240 Imm = o.Imm;
241 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000242 case MemBarrierOpt:
243 MBOpt = o.MBOpt;
244 break;
Sean Callanan76264762010-04-02 22:27:05 +0000245 case Memory:
246 Mem = o.Mem;
247 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000248 case MSRMask:
249 MMask = o.MMask;
250 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000251 case ProcIFlags:
252 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000253 break;
254 case Shifter:
255 Shift = o.Shift;
256 break;
Sean Callanan76264762010-04-02 22:27:05 +0000257 }
258 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000259
Sean Callanan76264762010-04-02 22:27:05 +0000260 /// getStartLoc - Get the location of the first token of this operand.
261 SMLoc getStartLoc() const { return StartLoc; }
262 /// getEndLoc - Get the location of the last token of this operand.
263 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000264
Daniel Dunbar8462b302010-08-11 06:36:53 +0000265 ARMCC::CondCodes getCondCode() const {
266 assert(Kind == CondCode && "Invalid access!");
267 return CC.Val;
268 }
269
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000270 unsigned getCoproc() const {
271 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
272 return Cop.Val;
273 }
274
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000275 StringRef getToken() const {
276 assert(Kind == Token && "Invalid access!");
277 return StringRef(Tok.Data, Tok.Length);
278 }
279
280 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000281 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000282 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000283 }
284
Bill Wendling5fa22a12010-11-09 23:28:44 +0000285 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000286 assert((Kind == RegisterList || Kind == DPRRegisterList ||
287 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000288 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000289 }
290
Kevin Enderbycfe07242009-10-13 22:19:02 +0000291 const MCExpr *getImm() const {
292 assert(Kind == Immediate && "Invalid access!");
293 return Imm.Val;
294 }
295
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000296 ARM_MB::MemBOpt getMemBarrierOpt() const {
297 assert(Kind == MemBarrierOpt && "Invalid access!");
298 return MBOpt.Val;
299 }
300
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000301 ARM_PROC::IFlags getProcIFlags() const {
302 assert(Kind == ProcIFlags && "Invalid access!");
303 return IFlags.Val;
304 }
305
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000306 unsigned getMSRMask() const {
307 assert(Kind == MSRMask && "Invalid access!");
308 return MMask.Val;
309 }
310
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000311 /// @name Memory Operand Accessors
312 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000313 ARMII::AddrMode getMemAddrMode() const {
314 return Mem.AddrMode;
315 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000316 unsigned getMemBaseRegNum() const {
317 return Mem.BaseRegNum;
318 }
319 unsigned getMemOffsetRegNum() const {
320 assert(Mem.OffsetIsReg && "Invalid access!");
321 return Mem.Offset.RegNum;
322 }
323 const MCExpr *getMemOffset() const {
324 assert(!Mem.OffsetIsReg && "Invalid access!");
325 return Mem.Offset.Value;
326 }
327 unsigned getMemOffsetRegShifted() const {
328 assert(Mem.OffsetIsReg && "Invalid access!");
329 return Mem.OffsetRegShifted;
330 }
331 const MCExpr *getMemShiftAmount() const {
332 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
333 return Mem.ShiftAmount;
334 }
Owen Anderson00828302011-03-18 22:50:18 +0000335 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000336 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
337 return Mem.ShiftType;
338 }
339 bool getMemPreindexed() const { return Mem.Preindexed; }
340 bool getMemPostindexed() const { return Mem.Postindexed; }
341 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
342 bool getMemNegative() const { return Mem.Negative; }
343 bool getMemWriteback() const { return Mem.Writeback; }
344
345 /// @}
346
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000347 bool isCoprocNum() const { return Kind == CoprocNum; }
348 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000349 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000350 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000351 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000352 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000353 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000354 bool isDPRRegList() const { return Kind == DPRRegisterList; }
355 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000356 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000357 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000358 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000359 bool isShifter() const { return Kind == Shifter; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000360 bool isMemMode2() const {
361 if (getMemAddrMode() != ARMII::AddrMode2)
362 return false;
363
364 if (getMemOffsetIsReg())
365 return true;
366
367 if (getMemNegative() &&
368 !(getMemPostindexed() || getMemPreindexed()))
369 return false;
370
371 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
372 if (!CE) return false;
373 int64_t Value = CE->getValue();
374
375 // The offset must be in the range 0-4095 (imm12).
376 if (Value > 4095 || Value < -4095)
377 return false;
378
379 return true;
380 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000381 bool isMemMode3() const {
382 if (getMemAddrMode() != ARMII::AddrMode3)
383 return false;
384
385 if (getMemOffsetIsReg()) {
386 if (getMemOffsetRegShifted())
387 return false; // No shift with offset reg allowed
388 return true;
389 }
390
391 if (getMemNegative() &&
392 !(getMemPostindexed() || getMemPreindexed()))
393 return false;
394
395 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
396 if (!CE) return false;
397 int64_t Value = CE->getValue();
398
399 // The offset must be in the range 0-255 (imm8).
400 if (Value > 255 || Value < -255)
401 return false;
402
403 return true;
404 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000405 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000406 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
407 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000408 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000409
Daniel Dunbar4b462672011-01-18 05:55:27 +0000410 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000411 if (!CE) return false;
412
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000413 // The offset must be a multiple of 4 in the range 0-1020.
414 int64_t Value = CE->getValue();
415 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
416 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000417 bool isMemMode7() const {
418 if (!isMemory() ||
419 getMemPreindexed() ||
420 getMemPostindexed() ||
421 getMemOffsetIsReg() ||
422 getMemNegative() ||
423 getMemWriteback())
424 return false;
425
426 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
427 if (!CE) return false;
428
429 if (CE->getValue())
430 return false;
431
432 return true;
433 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000434 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000435 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000436 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000437 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000438 }
439 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000440 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000441 return false;
442
Daniel Dunbar4b462672011-01-18 05:55:27 +0000443 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000444 if (!CE) return false;
445
446 // The offset must be a multiple of 4 in the range 0-124.
447 uint64_t Value = CE->getValue();
448 return ((Value & 0x3) == 0 && Value <= 124);
449 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000450 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000451 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000452
453 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000454 // Add as immediates when possible. Null MCExpr = 0.
455 if (Expr == 0)
456 Inst.addOperand(MCOperand::CreateImm(0));
457 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000458 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
459 else
460 Inst.addOperand(MCOperand::CreateExpr(Expr));
461 }
462
Daniel Dunbar8462b302010-08-11 06:36:53 +0000463 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000464 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000465 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000466 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
467 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000468 }
469
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000470 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
471 assert(N == 1 && "Invalid number of operands!");
472 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
473 }
474
475 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
476 assert(N == 1 && "Invalid number of operands!");
477 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
478 }
479
Jim Grosbachd67641b2010-12-06 18:21:12 +0000480 void addCCOutOperands(MCInst &Inst, unsigned N) const {
481 assert(N == 1 && "Invalid number of operands!");
482 Inst.addOperand(MCOperand::CreateReg(getReg()));
483 }
484
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000485 void addRegOperands(MCInst &Inst, unsigned N) const {
486 assert(N == 1 && "Invalid number of operands!");
487 Inst.addOperand(MCOperand::CreateReg(getReg()));
488 }
489
Owen Anderson00828302011-03-18 22:50:18 +0000490 void addShifterOperands(MCInst &Inst, unsigned N) const {
491 assert(N == 1 && "Invalid number of operands!");
492 Inst.addOperand(MCOperand::CreateImm(
493 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
494 }
495
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000496 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000497 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000498 const SmallVectorImpl<unsigned> &RegList = getRegList();
499 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000500 I = RegList.begin(), E = RegList.end(); I != E; ++I)
501 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000502 }
503
Bill Wendling0f630752010-11-17 04:32:08 +0000504 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
505 addRegListOperands(Inst, N);
506 }
507
508 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
509 addRegListOperands(Inst, N);
510 }
511
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000512 void addImmOperands(MCInst &Inst, unsigned N) const {
513 assert(N == 1 && "Invalid number of operands!");
514 addExpr(Inst, getImm());
515 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000516
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000517 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
518 assert(N == 1 && "Invalid number of operands!");
519 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
520 }
521
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000522 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
523 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
524 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
525
526 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000527 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000528 assert((CE || CE->getValue() == 0) &&
529 "No offset operand support in mode 7");
530 }
531
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000532 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
533 assert(isMemMode2() && "Invalid mode or number of operands!");
534 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
535 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
536
537 if (getMemOffsetIsReg()) {
538 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
539
540 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
541 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
542 int64_t ShiftAmount = 0;
543
544 if (getMemOffsetRegShifted()) {
545 ShOpc = getMemShiftType();
546 const MCConstantExpr *CE =
547 dyn_cast<MCConstantExpr>(getMemShiftAmount());
548 ShiftAmount = CE->getValue();
549 }
550
551 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
552 ShOpc, IdxMode)));
553 return;
554 }
555
556 // Create a operand placeholder to always yield the same number of operands.
557 Inst.addOperand(MCOperand::CreateReg(0));
558
559 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
560 // the difference?
561 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
562 assert(CE && "Non-constant mode 2 offset operand!");
563 int64_t Offset = CE->getValue();
564
565 if (Offset >= 0)
566 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
567 Offset, ARM_AM::no_shift, IdxMode)));
568 else
569 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
570 -Offset, ARM_AM::no_shift, IdxMode)));
571 }
572
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000573 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
574 assert(isMemMode3() && "Invalid mode or number of operands!");
575 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
576 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
577
578 if (getMemOffsetIsReg()) {
579 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
580
581 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
582 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
583 IdxMode)));
584 return;
585 }
586
587 // Create a operand placeholder to always yield the same number of operands.
588 Inst.addOperand(MCOperand::CreateReg(0));
589
590 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
591 // the difference?
592 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
593 assert(CE && "Non-constant mode 3 offset operand!");
594 int64_t Offset = CE->getValue();
595
596 if (Offset >= 0)
597 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
598 Offset, IdxMode)));
599 else
600 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
601 -Offset, IdxMode)));
602 }
603
Chris Lattner14b93852010-10-29 00:27:31 +0000604 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
605 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000606
Daniel Dunbar4b462672011-01-18 05:55:27 +0000607 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
608 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000609
Jim Grosbach80eb2332010-10-29 17:41:25 +0000610 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
611 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000612 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000613 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000614
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000615 // The MCInst offset operand doesn't include the low two bits (like
616 // the instruction encoding).
617 int64_t Offset = CE->getValue() / 4;
618 if (Offset >= 0)
619 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
620 Offset)));
621 else
622 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
623 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000624 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000625
Bill Wendlingf4caf692010-12-14 03:36:38 +0000626 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
627 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000628 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
629 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000630 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000631
Bill Wendlingf4caf692010-12-14 03:36:38 +0000632 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
633 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000634 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
635 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000636 assert(CE && "Non-constant mode offset operand!");
637 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000638 }
639
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000640 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
641 assert(N == 1 && "Invalid number of operands!");
642 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
643 }
644
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000645 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
646 assert(N == 1 && "Invalid number of operands!");
647 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
648 }
649
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000650 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000651
Chris Lattner3a697562010-10-28 17:20:03 +0000652 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
653 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000654 Op->CC.Val = CC;
655 Op->StartLoc = S;
656 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000657 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000658 }
659
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000660 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
661 ARMOperand *Op = new ARMOperand(CoprocNum);
662 Op->Cop.Val = CopVal;
663 Op->StartLoc = S;
664 Op->EndLoc = S;
665 return Op;
666 }
667
668 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
669 ARMOperand *Op = new ARMOperand(CoprocReg);
670 Op->Cop.Val = CopVal;
671 Op->StartLoc = S;
672 Op->EndLoc = S;
673 return Op;
674 }
675
Jim Grosbachd67641b2010-12-06 18:21:12 +0000676 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
677 ARMOperand *Op = new ARMOperand(CCOut);
678 Op->Reg.RegNum = RegNum;
679 Op->StartLoc = S;
680 Op->EndLoc = S;
681 return Op;
682 }
683
Chris Lattner3a697562010-10-28 17:20:03 +0000684 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
685 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000686 Op->Tok.Data = Str.data();
687 Op->Tok.Length = Str.size();
688 Op->StartLoc = S;
689 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000690 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000691 }
692
Bill Wendling50d0f582010-11-18 23:43:05 +0000693 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000694 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000695 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000696 Op->StartLoc = S;
697 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000698 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000699 }
700
Owen Anderson00828302011-03-18 22:50:18 +0000701 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
702 SMLoc S, SMLoc E) {
703 ARMOperand *Op = new ARMOperand(Shifter);
704 Op->Shift.ShiftTy = ShTy;
705 Op->StartLoc = S;
706 Op->EndLoc = E;
707 return Op;
708 }
709
Bill Wendling7729e062010-11-09 22:44:22 +0000710 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000711 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000712 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000713 KindTy Kind = RegisterList;
714
715 if (ARM::DPRRegClass.contains(Regs.front().first))
716 Kind = DPRRegisterList;
717 else if (ARM::SPRRegClass.contains(Regs.front().first))
718 Kind = SPRRegisterList;
719
720 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000721 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000722 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000723 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000724 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000725 Op->StartLoc = StartLoc;
726 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000727 return Op;
728 }
729
Chris Lattner3a697562010-10-28 17:20:03 +0000730 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
731 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000732 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000733 Op->StartLoc = S;
734 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000735 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000736 }
737
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000738 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
739 bool OffsetIsReg, const MCExpr *Offset,
740 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000741 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000742 const MCExpr *ShiftAmount, bool Preindexed,
743 bool Postindexed, bool Negative, bool Writeback,
744 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000745 assert((OffsetRegNum == -1 || OffsetIsReg) &&
746 "OffsetRegNum must imply OffsetIsReg!");
747 assert((!OffsetRegShifted || OffsetIsReg) &&
748 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000749 assert((Offset || OffsetIsReg) &&
750 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000751 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
752 "Cannot have shift amount without shifted register offset!");
753 assert((!Offset || !OffsetIsReg) &&
754 "Cannot have expression offset and register offset!");
755
Chris Lattner3a697562010-10-28 17:20:03 +0000756 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000757 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000758 Op->Mem.BaseRegNum = BaseRegNum;
759 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000760 if (OffsetIsReg)
761 Op->Mem.Offset.RegNum = OffsetRegNum;
762 else
763 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000764 Op->Mem.OffsetRegShifted = OffsetRegShifted;
765 Op->Mem.ShiftType = ShiftType;
766 Op->Mem.ShiftAmount = ShiftAmount;
767 Op->Mem.Preindexed = Preindexed;
768 Op->Mem.Postindexed = Postindexed;
769 Op->Mem.Negative = Negative;
770 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000771
Sean Callanan76264762010-04-02 22:27:05 +0000772 Op->StartLoc = S;
773 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000774 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000775 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000776
777 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
778 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
779 Op->MBOpt.Val = Opt;
780 Op->StartLoc = S;
781 Op->EndLoc = S;
782 return Op;
783 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000784
785 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
786 ARMOperand *Op = new ARMOperand(ProcIFlags);
787 Op->IFlags.Val = IFlags;
788 Op->StartLoc = S;
789 Op->EndLoc = S;
790 return Op;
791 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000792
793 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
794 ARMOperand *Op = new ARMOperand(MSRMask);
795 Op->MMask.Val = MMask;
796 Op->StartLoc = S;
797 Op->EndLoc = S;
798 return Op;
799 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000800};
801
802} // end anonymous namespace.
803
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000804void ARMOperand::dump(raw_ostream &OS) const {
805 switch (Kind) {
806 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000807 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000808 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000809 case CCOut:
810 OS << "<ccout " << getReg() << ">";
811 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000812 case CoprocNum:
813 OS << "<coprocessor number: " << getCoproc() << ">";
814 break;
815 case CoprocReg:
816 OS << "<coprocessor register: " << getCoproc() << ">";
817 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000818 case MSRMask:
819 OS << "<mask: " << getMSRMask() << ">";
820 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000821 case Immediate:
822 getImm()->print(OS);
823 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000824 case MemBarrierOpt:
825 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
826 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000827 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000828 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000829 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
830 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000831 if (getMemOffsetIsReg()) {
832 OS << " offset:<register " << getMemOffsetRegNum();
833 if (getMemOffsetRegShifted()) {
834 OS << " offset-shift-type:" << getMemShiftType();
835 OS << " offset-shift-amount:" << *getMemShiftAmount();
836 }
837 } else {
838 OS << " offset:" << *getMemOffset();
839 }
840 if (getMemOffsetIsReg())
841 OS << " (offset-is-reg)";
842 if (getMemPreindexed())
843 OS << " (pre-indexed)";
844 if (getMemPostindexed())
845 OS << " (post-indexed)";
846 if (getMemNegative())
847 OS << " (negative)";
848 if (getMemWriteback())
849 OS << " (writeback)";
850 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000851 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000852 case ProcIFlags: {
853 OS << "<ARM_PROC::";
854 unsigned IFlags = getProcIFlags();
855 for (int i=2; i >= 0; --i)
856 if (IFlags & (1 << i))
857 OS << ARM_PROC::IFlagsToString(1 << i);
858 OS << ">";
859 break;
860 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000861 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000862 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000863 break;
Owen Anderson00828302011-03-18 22:50:18 +0000864 case Shifter:
865 OS << "<shifter " << getShiftOpcStr(Shift.ShiftTy) << ">";
866 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000867 case RegisterList:
868 case DPRRegisterList:
869 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000870 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000871
Bill Wendling5fa22a12010-11-09 23:28:44 +0000872 const SmallVectorImpl<unsigned> &RegList = getRegList();
873 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000874 I = RegList.begin(), E = RegList.end(); I != E; ) {
875 OS << *I;
876 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000877 }
878
879 OS << ">";
880 break;
881 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000882 case Token:
883 OS << "'" << getToken() << "'";
884 break;
885 }
886}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000887
888/// @name Auto-generated Match Functions
889/// {
890
891static unsigned MatchRegisterName(StringRef Name);
892
893/// }
894
Bob Wilson69df7232011-02-03 21:46:10 +0000895bool ARMAsmParser::ParseRegister(unsigned &RegNo,
896 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000897 RegNo = TryParseRegister();
898
899 return (RegNo == (unsigned)-1);
900}
901
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000902/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000903/// and if it is a register name the token is eaten and the register number is
904/// returned. Otherwise return -1.
905///
906int ARMAsmParser::TryParseRegister() {
907 const AsmToken &Tok = Parser.getTok();
908 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000909
Chris Lattnere5658fa2010-10-30 04:09:10 +0000910 // FIXME: Validate register for the current architecture; we have to do
911 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000912 std::string upperCase = Tok.getString().str();
913 std::string lowerCase = LowercaseString(upperCase);
914 unsigned RegNum = MatchRegisterName(lowerCase);
915 if (!RegNum) {
916 RegNum = StringSwitch<unsigned>(lowerCase)
917 .Case("r13", ARM::SP)
918 .Case("r14", ARM::LR)
919 .Case("r15", ARM::PC)
920 .Case("ip", ARM::R12)
921 .Default(0);
922 }
923 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000924
Chris Lattnere5658fa2010-10-30 04:09:10 +0000925 Parser.Lex(); // Eat identifier token.
926 return RegNum;
927}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000928
Owen Anderson00828302011-03-18 22:50:18 +0000929/// Try to parse a register name. The token must be an Identifier when called,
930/// and if it is a register name the token is eaten and the register number is
931/// returned. Otherwise return -1.
932///
933bool ARMAsmParser::TryParseShiftRegister(
934 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
935 SMLoc S = Parser.getTok().getLoc();
936 const AsmToken &Tok = Parser.getTok();
937 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
938
939 std::string upperCase = Tok.getString().str();
940 std::string lowerCase = LowercaseString(upperCase);
941 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
942 .Case("lsl", ARM_AM::lsl)
943 .Case("lsr", ARM_AM::lsr)
944 .Case("asr", ARM_AM::asr)
945 .Case("ror", ARM_AM::ror)
946 .Case("rrx", ARM_AM::rrx)
947 .Default(ARM_AM::no_shift);
948
949 if (ShiftTy == ARM_AM::no_shift)
950 return true;
951
952 Parser.Lex(); // Eat shift-type operand;
953 int RegNum = TryParseRegister();
954 if (RegNum == -1)
955 return Error(Parser.getTok().getLoc(), "register expected");
956
957 Operands.push_back(ARMOperand::CreateReg(RegNum,S, Parser.getTok().getLoc()));
958 Operands.push_back(ARMOperand::CreateShifter(ShiftTy,
959 S, Parser.getTok().getLoc()));
960
961 return false;
962}
963
964
Bill Wendling50d0f582010-11-18 23:43:05 +0000965/// Try to parse a register name. The token must be an Identifier when called.
966/// If it's a register, an AsmOperand is created. Another AsmOperand is created
967/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000968///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000969/// TODO this is likely to change to allow different register types and or to
970/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000971bool ARMAsmParser::
972TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000973 SMLoc S = Parser.getTok().getLoc();
974 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000975 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000976 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000977
Bill Wendling50d0f582010-11-18 23:43:05 +0000978 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000979
Chris Lattnere5658fa2010-10-30 04:09:10 +0000980 const AsmToken &ExclaimTok = Parser.getTok();
981 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000982 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
983 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000984 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000985 }
986
Bill Wendling50d0f582010-11-18 23:43:05 +0000987 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000988}
989
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000990/// MatchCoprocessorOperandName - Try to parse an coprocessor related
991/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
992/// "c5", ...
993static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000994 // Use the same layout as the tablegen'erated register name matcher. Ugly,
995 // but efficient.
996 switch (Name.size()) {
997 default: break;
998 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000999 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001000 return -1;
1001 switch (Name[1]) {
1002 default: return -1;
1003 case '0': return 0;
1004 case '1': return 1;
1005 case '2': return 2;
1006 case '3': return 3;
1007 case '4': return 4;
1008 case '5': return 5;
1009 case '6': return 6;
1010 case '7': return 7;
1011 case '8': return 8;
1012 case '9': return 9;
1013 }
1014 break;
1015 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001016 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001017 return -1;
1018 switch (Name[2]) {
1019 default: return -1;
1020 case '0': return 10;
1021 case '1': return 11;
1022 case '2': return 12;
1023 case '3': return 13;
1024 case '4': return 14;
1025 case '5': return 15;
1026 }
1027 break;
1028 }
1029
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001030 return -1;
1031}
1032
Jim Grosbachf922c472011-02-12 01:34:40 +00001033/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001034/// token must be an Identifier when called, and if it is a coprocessor
1035/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001036ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1037tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001038 SMLoc S = Parser.getTok().getLoc();
1039 const AsmToken &Tok = Parser.getTok();
1040 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1041
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001042 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001043 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001044 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001045
1046 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001047 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001048 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001049}
1050
Jim Grosbachf922c472011-02-12 01:34:40 +00001051/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001052/// token must be an Identifier when called, and if it is a coprocessor
1053/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001054ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1055tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001056 SMLoc S = Parser.getTok().getLoc();
1057 const AsmToken &Tok = Parser.getTok();
1058 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1059
1060 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1061 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001062 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001063
1064 Parser.Lex(); // Eat identifier token.
1065 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001066 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001067}
1068
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001069/// Parse a register list, return it if successful else return null. The first
1070/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001071bool ARMAsmParser::
1072ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001073 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001074 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001075 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001076
Bill Wendling7729e062010-11-09 22:44:22 +00001077 // Read the rest of the registers in the list.
1078 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001079 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001080
Bill Wendling7729e062010-11-09 22:44:22 +00001081 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001082 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001083 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001084
Sean Callanan18b83232010-01-19 21:44:56 +00001085 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001086 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001087 if (RegTok.isNot(AsmToken::Identifier)) {
1088 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001089 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001090 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001091
Bill Wendling1d6a2652010-11-06 10:40:24 +00001092 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001093 if (RegNum == -1) {
1094 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001095 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001096 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001097
Bill Wendlinge7176102010-11-06 22:36:58 +00001098 if (IsRange) {
1099 int Reg = PrevRegNum;
1100 do {
1101 ++Reg;
1102 Registers.push_back(std::make_pair(Reg, RegLoc));
1103 } while (Reg != RegNum);
1104 } else {
1105 Registers.push_back(std::make_pair(RegNum, RegLoc));
1106 }
1107
1108 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001109 } while (Parser.getTok().is(AsmToken::Comma) ||
1110 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001111
1112 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001113 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001114 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1115 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001116 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001117 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001118
Bill Wendlinge7176102010-11-06 22:36:58 +00001119 SMLoc E = RCurlyTok.getLoc();
1120 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001121
Bill Wendlinge7176102010-11-06 22:36:58 +00001122 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001123 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001124 RI = Registers.begin(), RE = Registers.end();
1125
Bill Wendling7caebff2011-01-12 21:20:59 +00001126 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001127 bool EmittedWarning = false;
1128
Bill Wendling7caebff2011-01-12 21:20:59 +00001129 DenseMap<unsigned, bool> RegMap;
1130 RegMap[HighRegNum] = true;
1131
Bill Wendlinge7176102010-11-06 22:36:58 +00001132 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001133 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001134 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001135
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001136 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001137 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001138 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001139 }
1140
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001141 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001142 Warning(RegInfo.second,
1143 "register not in ascending order in register list");
1144
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001145 RegMap[Reg] = true;
1146 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001147 }
1148
Bill Wendling50d0f582010-11-18 23:43:05 +00001149 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1150 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001151}
1152
Jim Grosbachf922c472011-02-12 01:34:40 +00001153/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1154ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1155tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001156 SMLoc S = Parser.getTok().getLoc();
1157 const AsmToken &Tok = Parser.getTok();
1158 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1159 StringRef OptStr = Tok.getString();
1160
1161 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1162 .Case("sy", ARM_MB::SY)
1163 .Case("st", ARM_MB::ST)
1164 .Case("ish", ARM_MB::ISH)
1165 .Case("ishst", ARM_MB::ISHST)
1166 .Case("nsh", ARM_MB::NSH)
1167 .Case("nshst", ARM_MB::NSHST)
1168 .Case("osh", ARM_MB::OSH)
1169 .Case("oshst", ARM_MB::OSHST)
1170 .Default(~0U);
1171
1172 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001173 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001174
1175 Parser.Lex(); // Eat identifier token.
1176 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001177 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001178}
1179
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001180/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001181ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1182tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1183 SMLoc S = Parser.getTok().getLoc();
1184 const AsmToken &Tok = Parser.getTok();
1185 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1186 StringRef IFlagsStr = Tok.getString();
1187
1188 unsigned IFlags = 0;
1189 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1190 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1191 .Case("a", ARM_PROC::A)
1192 .Case("i", ARM_PROC::I)
1193 .Case("f", ARM_PROC::F)
1194 .Default(~0U);
1195
1196 // If some specific iflag is already set, it means that some letter is
1197 // present more than once, this is not acceptable.
1198 if (Flag == ~0U || (IFlags & Flag))
1199 return MatchOperand_NoMatch;
1200
1201 IFlags |= Flag;
1202 }
1203
1204 Parser.Lex(); // Eat identifier token.
1205 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1206 return MatchOperand_Success;
1207}
1208
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001209/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1210ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1211tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1212 SMLoc S = Parser.getTok().getLoc();
1213 const AsmToken &Tok = Parser.getTok();
1214 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1215 StringRef Mask = Tok.getString();
1216
1217 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1218 size_t Start = 0, Next = Mask.find('_');
1219 StringRef Flags = "";
1220 StringRef SpecReg = Mask.slice(Start, Next);
1221 if (Next != StringRef::npos)
1222 Flags = Mask.slice(Next+1, Mask.size());
1223
1224 // FlagsVal contains the complete mask:
1225 // 3-0: Mask
1226 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1227 unsigned FlagsVal = 0;
1228
1229 if (SpecReg == "apsr") {
1230 FlagsVal = StringSwitch<unsigned>(Flags)
1231 .Case("nzcvq", 0x8) // same as CPSR_c
1232 .Case("g", 0x4) // same as CPSR_s
1233 .Case("nzcvqg", 0xc) // same as CPSR_fs
1234 .Default(~0U);
1235
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001236 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001237 if (!Flags.empty())
1238 return MatchOperand_NoMatch;
1239 else
1240 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001241 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001242 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
1243 for (int i = 0, e = Flags.size(); i != e; ++i) {
1244 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1245 .Case("c", 1)
1246 .Case("x", 2)
1247 .Case("s", 4)
1248 .Case("f", 8)
1249 .Default(~0U);
1250
1251 // If some specific flag is already set, it means that some letter is
1252 // present more than once, this is not acceptable.
1253 if (FlagsVal == ~0U || (FlagsVal & Flag))
1254 return MatchOperand_NoMatch;
1255 FlagsVal |= Flag;
1256 }
1257 } else // No match for special register.
1258 return MatchOperand_NoMatch;
1259
1260 // Special register without flags are equivalent to "fc" flags.
1261 if (!FlagsVal)
1262 FlagsVal = 0x9;
1263
1264 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1265 if (SpecReg == "spsr")
1266 FlagsVal |= 16;
1267
1268 Parser.Lex(); // Eat identifier token.
1269 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1270 return MatchOperand_Success;
1271}
1272
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001273/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1274ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1275tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001276 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001277
1278 if (ParseMemory(Operands, ARMII::AddrMode2))
1279 return MatchOperand_NoMatch;
1280
1281 return MatchOperand_Success;
1282}
1283
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001284/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1285ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1286tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1287 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1288
1289 if (ParseMemory(Operands, ARMII::AddrMode3))
1290 return MatchOperand_NoMatch;
1291
1292 return MatchOperand_Success;
1293}
1294
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001295/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1296/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1297/// when they refer multiple MIOperands inside a single one.
1298bool ARMAsmParser::
1299CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1300 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1301 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1302
1303 // Create a writeback register dummy placeholder.
1304 Inst.addOperand(MCOperand::CreateImm(0));
1305
1306 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1307 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1308 return true;
1309}
1310
1311/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1312/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1313/// when they refer multiple MIOperands inside a single one.
1314bool ARMAsmParser::
1315CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1316 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1317 // Create a writeback register dummy placeholder.
1318 Inst.addOperand(MCOperand::CreateImm(0));
1319 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1320 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1321 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1322 return true;
1323}
1324
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001325/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1326/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1327/// when they refer multiple MIOperands inside a single one.
1328bool ARMAsmParser::
1329CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1330 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1331 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1332
1333 // Create a writeback register dummy placeholder.
1334 Inst.addOperand(MCOperand::CreateImm(0));
1335
1336 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1337 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1338 return true;
1339}
1340
1341/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1342/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1343/// when they refer multiple MIOperands inside a single one.
1344bool ARMAsmParser::
1345CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1346 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1347 // Create a writeback register dummy placeholder.
1348 Inst.addOperand(MCOperand::CreateImm(0));
1349 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1350 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1351 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1352 return true;
1353}
1354
Bill Wendlinge7176102010-11-06 22:36:58 +00001355/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001356/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001357///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001358/// TODO Only preindexing and postindexing addressing are started, unindexed
1359/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001360bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001361ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1362 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001363 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001364 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001365 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001366 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001367 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001368
Sean Callanan18b83232010-01-19 21:44:56 +00001369 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001370 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1371 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001372 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001373 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001374 int BaseRegNum = TryParseRegister();
1375 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001376 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001377 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001378 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001379
Daniel Dunbar05710932011-01-18 05:34:17 +00001380 // The next token must either be a comma or a closing bracket.
1381 const AsmToken &Tok = Parser.getTok();
1382 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1383 return true;
1384
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001385 bool Preindexed = false;
1386 bool Postindexed = false;
1387 bool OffsetIsReg = false;
1388 bool Negative = false;
1389 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001390 ARMOperand *WBOp = 0;
1391 int OffsetRegNum = -1;
1392 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001393 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001394 const MCExpr *ShiftAmount = 0;
1395 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001396
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001397 // First look for preindexed address forms, that is after the "[Rn" we now
1398 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001399 if (Tok.is(AsmToken::Comma)) {
1400 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001401 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001402
Chris Lattner550276e2010-10-28 20:52:15 +00001403 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1404 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001405 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001406 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001407 if (RBracTok.isNot(AsmToken::RBrac)) {
1408 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001409 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001410 }
Sean Callanan76264762010-04-02 22:27:05 +00001411 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001412 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001413
Sean Callanan18b83232010-01-19 21:44:56 +00001414 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001415 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001416 // None of addrmode3 instruction uses "!"
1417 if (AddrMode == ARMII::AddrMode3)
1418 return true;
1419
Bill Wendling50d0f582010-11-18 23:43:05 +00001420 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1421 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001422 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001423 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001424 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1425 if (AddrMode == ARMII::AddrMode2)
1426 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001427 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001428 } else {
1429 // The "[Rn" we have so far was not followed by a comma.
1430
Jim Grosbach80eb2332010-10-29 17:41:25 +00001431 // If there's anything other than the right brace, this is a post indexing
1432 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001433 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001434 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001435
Sean Callanan18b83232010-01-19 21:44:56 +00001436 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001437
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001438 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001439 Postindexed = true;
1440 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001441
Chris Lattner550276e2010-10-28 20:52:15 +00001442 if (NextTok.isNot(AsmToken::Comma)) {
1443 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001444 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001445 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001446
Sean Callananb9a25b72010-01-19 20:27:46 +00001447 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001448
Chris Lattner550276e2010-10-28 20:52:15 +00001449 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001450 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001451 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001452 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001453 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001454 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001455
1456 // Force Offset to exist if used.
1457 if (!OffsetIsReg) {
1458 if (!Offset)
1459 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001460 } else {
1461 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1462 Error(E, "shift amount not supported");
1463 return true;
1464 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001465 }
1466
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001467 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1468 Offset, OffsetRegNum, OffsetRegShifted,
1469 ShiftType, ShiftAmount, Preindexed,
1470 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001471 if (WBOp)
1472 Operands.push_back(WBOp);
1473
1474 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001475}
1476
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001477/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1478/// we will parse the following (were +/- means that a plus or minus is
1479/// optional):
1480/// +/-Rm
1481/// +/-Rm, shift
1482/// #offset
1483/// we return false on success or an error otherwise.
1484bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001485 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001486 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001487 const MCExpr *&ShiftAmount,
1488 const MCExpr *&Offset,
1489 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001490 int &OffsetRegNum,
1491 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001492 Negative = false;
1493 OffsetRegShifted = false;
1494 OffsetIsReg = false;
1495 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001496 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001497 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001498 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001499 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001500 else if (NextTok.is(AsmToken::Minus)) {
1501 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001502 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001503 }
1504 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001505 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001506 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001507 SMLoc CurLoc = OffsetRegTok.getLoc();
1508 OffsetRegNum = TryParseRegister();
1509 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001510 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001511 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001512 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001513 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001514
Bill Wendling12f40e92010-11-06 10:51:53 +00001515 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001516 if (OffsetRegNum != -1) {
1517 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001518 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001519 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001520 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001521
Sean Callanan18b83232010-01-19 21:44:56 +00001522 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001523 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001524 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001525 OffsetRegShifted = true;
1526 }
1527 }
1528 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1529 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001530 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001531 if (HashTok.isNot(AsmToken::Hash))
1532 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001533
Sean Callananb9a25b72010-01-19 20:27:46 +00001534 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001535
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001536 if (getParser().ParseExpression(Offset))
1537 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001538 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001539 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001540 return false;
1541}
1542
1543/// ParseShift as one of these two:
1544/// ( lsl | lsr | asr | ror ) , # shift_amount
1545/// rrx
1546/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001547bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1548 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001549 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001550 if (Tok.isNot(AsmToken::Identifier))
1551 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001552 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001553 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001554 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001555 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001556 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001557 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001558 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001559 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001560 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001561 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001562 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001563 else
1564 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001565 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001566
1567 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001568 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001569 return false;
1570
1571 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001572 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001573 if (HashTok.isNot(AsmToken::Hash))
1574 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001575 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001576
1577 if (getParser().ParseExpression(ShiftAmount))
1578 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001579
1580 return false;
1581}
1582
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001583/// Parse a arm instruction operand. For now this parses the operand regardless
1584/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001585bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001586 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001587 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001588
1589 // Check if the current operand has a custom associated parser, if so, try to
1590 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001591 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1592 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001593 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001594 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1595 // there was a match, but an error occurred, in which case, just return that
1596 // the operand parsing failed.
1597 if (ResTy == MatchOperand_ParseFail)
1598 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001599
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001600 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001601 default:
1602 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001603 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001604 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001605 if (!TryParseRegisterWithWriteBack(Operands))
1606 return false;
Owen Anderson00828302011-03-18 22:50:18 +00001607 if (!TryParseShiftRegister(Operands))
1608 return false;
1609
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001610
1611 // Fall though for the Identifier case that is not a register or a
1612 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001613 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1614 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001615 // This was not a register so parse other operands that start with an
1616 // identifier (like labels) as expressions and create them as immediates.
1617 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001618 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001619 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001620 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001621 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001622 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1623 return false;
1624 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001625 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001626 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001627 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001628 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001629 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001630 // #42 -> immediate.
1631 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001632 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001633 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001634 const MCExpr *ImmVal;
1635 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001636 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001637 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001638 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1639 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001640 case AsmToken::Colon: {
1641 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001642 // FIXME: Check it's an expression prefix,
1643 // e.g. (FOO - :lower16:BAR) isn't legal.
1644 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001645 if (ParsePrefix(RefKind))
1646 return true;
1647
Evan Cheng75972122011-01-13 07:58:56 +00001648 const MCExpr *SubExprVal;
1649 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001650 return true;
1651
Evan Cheng75972122011-01-13 07:58:56 +00001652 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1653 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001654 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001655 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001656 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001657 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001658 }
1659}
1660
Evan Cheng75972122011-01-13 07:58:56 +00001661// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1662// :lower16: and :upper16:.
1663bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1664 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001665
1666 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001667 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001668 Parser.Lex(); // Eat ':'
1669
1670 if (getLexer().isNot(AsmToken::Identifier)) {
1671 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1672 return true;
1673 }
1674
1675 StringRef IDVal = Parser.getTok().getIdentifier();
1676 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001677 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001678 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001679 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001680 } else {
1681 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1682 return true;
1683 }
1684 Parser.Lex();
1685
1686 if (getLexer().isNot(AsmToken::Colon)) {
1687 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1688 return true;
1689 }
1690 Parser.Lex(); // Eat the last ':'
1691 return false;
1692}
1693
1694const MCExpr *
1695ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1696 MCSymbolRefExpr::VariantKind Variant) {
1697 // Recurse over the given expression, rebuilding it to apply the given variant
1698 // to the leftmost symbol.
1699 if (Variant == MCSymbolRefExpr::VK_None)
1700 return E;
1701
1702 switch (E->getKind()) {
1703 case MCExpr::Target:
1704 llvm_unreachable("Can't handle target expr yet");
1705 case MCExpr::Constant:
1706 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1707
1708 case MCExpr::SymbolRef: {
1709 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1710
1711 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1712 return 0;
1713
1714 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1715 }
1716
1717 case MCExpr::Unary:
1718 llvm_unreachable("Can't handle unary expressions yet");
1719
1720 case MCExpr::Binary: {
1721 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1722 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1723 const MCExpr *RHS = BE->getRHS();
1724 if (!LHS)
1725 return 0;
1726
1727 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1728 }
1729 }
1730
1731 assert(0 && "Invalid expression kind!");
1732 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001733}
1734
Daniel Dunbar352e1482011-01-11 15:59:50 +00001735/// \brief Given a mnemonic, split out possible predication code and carry
1736/// setting letters to form a canonical mnemonic and flags.
1737//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001738// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001739static StringRef SplitMnemonic(StringRef Mnemonic,
1740 unsigned &PredicationCode,
1741 bool &CarrySetting,
1742 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001743 PredicationCode = ARMCC::AL;
1744 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001745 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001746
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001747 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001748 //
1749 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001750 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1751 Mnemonic == "movs" ||
1752 Mnemonic == "svc" ||
1753 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1754 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1755 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1756 Mnemonic == "vclt" ||
1757 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1758 Mnemonic == "vcle" ||
1759 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1760 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1761 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001762 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001763
Daniel Dunbar352e1482011-01-11 15:59:50 +00001764 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001765 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001766 .Case("eq", ARMCC::EQ)
1767 .Case("ne", ARMCC::NE)
1768 .Case("hs", ARMCC::HS)
1769 .Case("lo", ARMCC::LO)
1770 .Case("mi", ARMCC::MI)
1771 .Case("pl", ARMCC::PL)
1772 .Case("vs", ARMCC::VS)
1773 .Case("vc", ARMCC::VC)
1774 .Case("hi", ARMCC::HI)
1775 .Case("ls", ARMCC::LS)
1776 .Case("ge", ARMCC::GE)
1777 .Case("lt", ARMCC::LT)
1778 .Case("gt", ARMCC::GT)
1779 .Case("le", ARMCC::LE)
1780 .Case("al", ARMCC::AL)
1781 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001782 if (CC != ~0U) {
1783 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001784 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001785 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001786
Daniel Dunbar352e1482011-01-11 15:59:50 +00001787 // Next, determine if we have a carry setting bit. We explicitly ignore all
1788 // the instructions we know end in 's'.
1789 if (Mnemonic.endswith("s") &&
1790 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1791 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1792 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1793 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1794 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1795 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1796 CarrySetting = true;
1797 }
1798
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001799 // The "cps" instruction can have a interrupt mode operand which is glued into
1800 // the mnemonic. Check if this is the case, split it and parse the imod op
1801 if (Mnemonic.startswith("cps")) {
1802 // Split out any imod code.
1803 unsigned IMod =
1804 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1805 .Case("ie", ARM_PROC::IE)
1806 .Case("id", ARM_PROC::ID)
1807 .Default(~0U);
1808 if (IMod != ~0U) {
1809 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1810 ProcessorIMod = IMod;
1811 }
1812 }
1813
Daniel Dunbar352e1482011-01-11 15:59:50 +00001814 return Mnemonic;
1815}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001816
1817/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1818/// inclusion of carry set or predication code operands.
1819//
1820// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001821void ARMAsmParser::
1822GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1823 bool &CanAcceptPredicationCode) {
1824 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1825
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001826 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1827 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1828 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1829 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1830 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1831 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1832 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1833 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1834 CanAcceptCarrySet = true;
1835 } else {
1836 CanAcceptCarrySet = false;
1837 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001838
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001839 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1840 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1841 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1842 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001843 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001844 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001845 CanAcceptPredicationCode = false;
1846 } else {
1847 CanAcceptPredicationCode = true;
1848 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001849
1850 if (isThumb)
1851 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001852 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001853 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001854}
1855
1856/// Parse an arm instruction mnemonic followed by its operands.
1857bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1858 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1859 // Create the leading tokens for the mnemonic, split by '.' characters.
1860 size_t Start = 0, Next = Name.find('.');
1861 StringRef Head = Name.slice(Start, Next);
1862
Daniel Dunbar352e1482011-01-11 15:59:50 +00001863 // Split out the predication code and carry setting flag from the mnemonic.
1864 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001865 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001866 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001867 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1868 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001869
Chris Lattner3a697562010-10-28 17:20:03 +00001870 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001871
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001872 // Next, add the CCOut and ConditionCode operands, if needed.
1873 //
1874 // For mnemonics which can ever incorporate a carry setting bit or predication
1875 // code, our matching model involves us always generating CCOut and
1876 // ConditionCode operands to match the mnemonic "as written" and then we let
1877 // the matcher deal with finding the right instruction or generating an
1878 // appropriate error.
1879 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1880 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1881
1882 // Add the carry setting operand, if necessary.
1883 //
1884 // FIXME: It would be awesome if we could somehow invent a location such that
1885 // match errors on this operand would print a nice diagnostic about how the
1886 // 's' character in the mnemonic resulted in a CCOut operand.
1887 if (CanAcceptCarrySet) {
1888 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1889 NameLoc));
1890 } else {
1891 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1892 // misspelled another mnemonic).
1893
1894 // FIXME: Issue a nice error.
1895 }
1896
1897 // Add the predication code operand, if necessary.
1898 if (CanAcceptPredicationCode) {
1899 Operands.push_back(ARMOperand::CreateCondCode(
1900 ARMCC::CondCodes(PredicationCode), NameLoc));
1901 } else {
1902 // This mnemonic can't ever accept a predication code, but the user wrote
1903 // one (or misspelled another mnemonic).
1904
1905 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001906 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001907
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001908 // Add the processor imod operand, if necessary.
1909 if (ProcessorIMod) {
1910 Operands.push_back(ARMOperand::CreateImm(
1911 MCConstantExpr::Create(ProcessorIMod, getContext()),
1912 NameLoc, NameLoc));
1913 } else {
1914 // This mnemonic can't ever accept a imod, but the user wrote
1915 // one (or misspelled another mnemonic).
1916
1917 // FIXME: Issue a nice error.
1918 }
1919
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001920 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001921 while (Next != StringRef::npos) {
1922 Start = Next;
1923 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001924 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001925
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001926 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001927 }
1928
1929 // Read the remaining operands.
1930 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001931 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001932 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001933 Parser.EatToEndOfStatement();
1934 return true;
1935 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001936
1937 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001938 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001939
1940 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001941 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001942 Parser.EatToEndOfStatement();
1943 return true;
1944 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001945 }
1946 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001947
Chris Lattnercbf8a982010-09-11 16:18:25 +00001948 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1949 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001950 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001951 }
Bill Wendling146018f2010-11-06 21:42:12 +00001952
Chris Lattner34e53142010-09-08 05:10:46 +00001953 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001954 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001955}
1956
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001957bool ARMAsmParser::
1958MatchAndEmitInstruction(SMLoc IDLoc,
1959 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1960 MCStreamer &Out) {
1961 MCInst Inst;
1962 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001963 MatchResultTy MatchResult, MatchResult2;
1964 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1965 if (MatchResult != Match_Success) {
1966 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1967 // that does not update the condition codes. So try adding a CCOut operand
1968 // with a value of reg0.
1969 if (MatchResult == Match_InvalidOperand) {
1970 Operands.insert(Operands.begin() + 1,
1971 ARMOperand::CreateCCOut(0,
1972 ((ARMOperand*)Operands[0])->getStartLoc()));
1973 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1974 if (MatchResult2 == Match_Success)
1975 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001976 else {
1977 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001978 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001979 delete CCOut;
1980 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001981 }
1982 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1983 // that updates the condition codes if it ends in 's'. So see if the
1984 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1985 // operand with a value of CPSR.
1986 else if(MatchResult == Match_MnemonicFail) {
1987 // Get the instruction mnemonic, which is the first token.
1988 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1989 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1990 // removed the 's' from the mnemonic for matching.
1991 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1992 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001993 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1994 Operands.erase(Operands.begin());
1995 delete OldMnemonic;
1996 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001997 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1998 Operands.insert(Operands.begin() + 1,
1999 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2000 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2001 if (MatchResult2 == Match_Success)
2002 MatchResult = Match_Success;
2003 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002004 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2005 Operands.erase(Operands.begin());
2006 delete OldMnemonic;
2007 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002008 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002009 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2010 Operands.erase(Operands.begin() + 1);
2011 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002012 }
2013 }
2014 }
2015 }
2016 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002017 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002018 Out.EmitInstruction(Inst);
2019 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002020 case Match_MissingFeature:
2021 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2022 return true;
2023 case Match_InvalidOperand: {
2024 SMLoc ErrorLoc = IDLoc;
2025 if (ErrorInfo != ~0U) {
2026 if (ErrorInfo >= Operands.size())
2027 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002028
Chris Lattnere73d4f82010-10-28 21:41:58 +00002029 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2030 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2031 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002032
Chris Lattnere73d4f82010-10-28 21:41:58 +00002033 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002034 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002035 case Match_MnemonicFail:
2036 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002037 case Match_ConversionFail:
2038 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002039 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002040
Eric Christopherc223e2b2010-10-29 09:26:59 +00002041 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002042 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002043}
2044
Kevin Enderby515d5092009-10-15 20:48:48 +00002045/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002046bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2047 StringRef IDVal = DirectiveID.getIdentifier();
2048 if (IDVal == ".word")
2049 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002050 else if (IDVal == ".thumb")
2051 return ParseDirectiveThumb(DirectiveID.getLoc());
2052 else if (IDVal == ".thumb_func")
2053 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2054 else if (IDVal == ".code")
2055 return ParseDirectiveCode(DirectiveID.getLoc());
2056 else if (IDVal == ".syntax")
2057 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002058 return true;
2059}
2060
2061/// ParseDirectiveWord
2062/// ::= .word [ expression (, expression)* ]
2063bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2064 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2065 for (;;) {
2066 const MCExpr *Value;
2067 if (getParser().ParseExpression(Value))
2068 return true;
2069
Chris Lattneraaec2052010-01-19 19:46:13 +00002070 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002071
2072 if (getLexer().is(AsmToken::EndOfStatement))
2073 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002074
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002075 // FIXME: Improve diagnostic.
2076 if (getLexer().isNot(AsmToken::Comma))
2077 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002078 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002079 }
2080 }
2081
Sean Callananb9a25b72010-01-19 20:27:46 +00002082 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002083 return false;
2084}
2085
Kevin Enderby515d5092009-10-15 20:48:48 +00002086/// ParseDirectiveThumb
2087/// ::= .thumb
2088bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2089 if (getLexer().isNot(AsmToken::EndOfStatement))
2090 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002091 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002092
2093 // TODO: set thumb mode
2094 // TODO: tell the MC streamer the mode
2095 // getParser().getStreamer().Emit???();
2096 return false;
2097}
2098
2099/// ParseDirectiveThumbFunc
2100/// ::= .thumbfunc symbol_name
2101bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002102 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002103 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00002104 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002105 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00002106 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00002107 if (getLexer().isNot(AsmToken::EndOfStatement))
2108 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002109 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002110
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002111 // Mark symbol as a thumb symbol.
2112 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2113 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002114 return false;
2115}
2116
2117/// ParseDirectiveSyntax
2118/// ::= .syntax unified | divided
2119bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002120 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002121 if (Tok.isNot(AsmToken::Identifier))
2122 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002123 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002124 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002125 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002126 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002127 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002128 else
2129 return Error(L, "unrecognized syntax mode in .syntax directive");
2130
2131 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002132 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002133 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002134
2135 // TODO tell the MC streamer the mode
2136 // getParser().getStreamer().Emit???();
2137 return false;
2138}
2139
2140/// ParseDirectiveCode
2141/// ::= .code 16 | 32
2142bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002143 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002144 if (Tok.isNot(AsmToken::Integer))
2145 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002146 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002147 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002148 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002149 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002150 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002151 else
2152 return Error(L, "invalid operand to .code directive");
2153
2154 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002155 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002156 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002157
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002158 // FIXME: We need to be able switch subtargets at this point so that
2159 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
2160 // includes Feature_IsThumb or not to match the right instructions. This is
2161 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
2162 if (Val == 16){
2163 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
2164 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002165 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002166 }
2167 else{
2168 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
2169 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002170 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002171 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002172
Kevin Enderby515d5092009-10-15 20:48:48 +00002173 return false;
2174}
2175
Sean Callanan90b70972010-04-07 20:29:34 +00002176extern "C" void LLVMInitializeARMAsmLexer();
2177
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002178/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002179extern "C" void LLVMInitializeARMAsmParser() {
2180 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2181 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002182 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002183}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002184
Chris Lattner0692ee62010-09-06 19:11:01 +00002185#define GET_REGISTER_MATCHER
2186#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002187#include "ARMGenAsmMatcher.inc"