blob: ef611b21e72d87d95033bd65ad58a66520273f6d [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"
Evan Chengb72d2a92011-01-11 21:46:47 +000011#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000012#include "ARMSubtarget.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000013#include "MCTargetDesc/ARMAddressingModes.h"
14#include "MCTargetDesc/ARMMCExpr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
17#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Rafael Espindola64695402011-05-16 16:17:21 +000018#include "llvm/MC/MCAsmInfo.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000019#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000020#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000023#include "llvm/MC/MCSubtargetInfo.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000024#include "llvm/Target/TargetRegistry.h"
25#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000027#include "llvm/Support/raw_ostream.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000028#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000030#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000031#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000032#include "llvm/ADT/Twine.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000033
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000034using namespace llvm;
35
Chris Lattner3a697562010-10-28 17:20:03 +000036namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000037
38class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000039
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000040class ARMAsmParser : public TargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000041 MCSubtargetInfo &STI;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042 MCAsmParser &Parser;
43
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000044 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
46
47 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
49
Chris Lattnere5658fa2010-10-30 04:09:10 +000050 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000051 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000052 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach19906722011-07-13 18:49:30 +000053 int TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000054 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000055 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
56 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000057 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000058 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000059 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
60 MCSymbolRefExpr::VariantKind Variant);
61
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000062
Kevin Enderby9c41fa82009-10-30 22:55:57 +000063 bool ParseMemoryOffsetReg(bool &Negative,
64 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000065 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 const MCExpr *&ShiftAmount,
67 const MCExpr *&Offset,
68 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000069 int &OffsetRegNum,
70 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000071 bool ParseShift(enum ARM_AM::ShiftOpc &St,
72 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000073 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000074 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000075 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000076 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveSyntax(SMLoc L);
78
Chris Lattner7036f8b2010-09-29 01:42:58 +000079 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000080 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000081 MCStreamer &Out);
Jim Grosbach5f160572011-07-19 20:10:31 +000082 StringRef SplitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
83 bool &CarrySetting, unsigned &ProcessorIMod);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000084 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
85 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000086
Evan Chengebdeeab2011-07-08 01:53:10 +000087 bool isThumb() const {
88 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000089 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000090 }
Evan Chengebdeeab2011-07-08 01:53:10 +000091 bool isThumbOne() const {
Evan Chengffc0e732011-07-09 05:47:46 +000092 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000093 }
Evan Cheng32869202011-07-08 22:36:29 +000094 void SwitchMode() {
Evan Chengffc0e732011-07-09 05:47:46 +000095 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
96 setAvailableFeatures(FB);
Evan Cheng32869202011-07-08 22:36:29 +000097 }
Evan Chengebdeeab2011-07-08 01:53:10 +000098
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000099 /// @name Auto-generated Match Functions
100 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000101
Chris Lattner0692ee62010-09-06 19:11:01 +0000102#define GET_ASSEMBLER_HEADER
103#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000104
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000105 /// }
106
Jim Grosbachf922c472011-02-12 01:34:40 +0000107 OperandMatchResultTy tryParseCoprocNumOperand(
108 SmallVectorImpl<MCParsedAsmOperand*>&);
109 OperandMatchResultTy tryParseCoprocRegOperand(
110 SmallVectorImpl<MCParsedAsmOperand*>&);
111 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000112 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000113 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000114 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000115 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000116 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000117 OperandMatchResultTy tryParseMemMode2Operand(
118 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000119 OperandMatchResultTy tryParseMemMode3Operand(
120 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbachf6c05252011-07-21 17:23:04 +0000121 OperandMatchResultTy parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &O,
122 StringRef Op, int Low, int High);
123 OperandMatchResultTy parsePKHLSLImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
124 return parsePKHImm(O, "lsl", 0, 31);
125 }
126 OperandMatchResultTy parsePKHASRImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
127 return parsePKHImm(O, "asr", 1, 32);
128 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000129 OperandMatchResultTy parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000130
131 // Asm Match Converter Methods
132 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
133 const SmallVectorImpl<MCParsedAsmOperand*> &);
134 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
135 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000136 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
137 const SmallVectorImpl<MCParsedAsmOperand*> &);
138 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
139 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000140
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000141public:
Evan Chengffc0e732011-07-09 05:47:46 +0000142 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
143 : TargetAsmParser(), STI(_STI), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000144 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000145
Evan Chengebdeeab2011-07-08 01:53:10 +0000146 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000147 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evan Chengebdeeab2011-07-08 01:53:10 +0000148 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000149
Benjamin Kramer38e59892010-07-14 22:38:02 +0000150 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000151 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000152 virtual bool ParseDirective(AsmToken DirectiveID);
153};
Jim Grosbach16c74252010-10-29 14:46:02 +0000154} // end anonymous namespace
155
Chris Lattner3a697562010-10-28 17:20:03 +0000156namespace {
157
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000158/// ARMOperand - Instances of this class represent a parsed ARM machine
159/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000160class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000161 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000162 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000163 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000164 CoprocNum,
165 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000166 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000167 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000168 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000169 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000170 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000171 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000172 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000173 DPRRegisterList,
174 SPRRegisterList,
Jim Grosbache8606dc2011-07-13 17:50:29 +0000175 ShiftedRegister,
Owen Anderson92a20222011-07-21 18:54:16 +0000176 ShiftedImmediate,
Owen Anderson00828302011-03-18 22:50:18 +0000177 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000178 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000179 } Kind;
180
Sean Callanan76264762010-04-02 22:27:05 +0000181 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000182 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000183
184 union {
185 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000186 ARMCC::CondCodes Val;
187 } CC;
188
189 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000190 ARM_MB::MemBOpt Val;
191 } MBOpt;
192
193 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000194 unsigned Val;
195 } Cop;
196
197 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000198 ARM_PROC::IFlags Val;
199 } IFlags;
200
201 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000202 unsigned Val;
203 } MMask;
204
205 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000206 const char *Data;
207 unsigned Length;
208 } Tok;
209
210 struct {
211 unsigned RegNum;
212 } Reg;
213
Bill Wendling8155e5b2010-11-06 22:19:43 +0000214 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000215 const MCExpr *Val;
216 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000217
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000218 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000219 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000220 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000221 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000222 union {
223 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
224 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
225 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000226 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000227 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000228 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000229 unsigned Preindexed : 1;
230 unsigned Postindexed : 1;
231 unsigned OffsetIsReg : 1;
232 unsigned Negative : 1; // only used when OffsetIsReg is true
233 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000234 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000235
236 struct {
237 ARM_AM::ShiftOpc ShiftTy;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000238 unsigned Imm;
Owen Anderson00828302011-03-18 22:50:18 +0000239 } Shift;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000240 struct {
241 ARM_AM::ShiftOpc ShiftTy;
242 unsigned SrcReg;
243 unsigned ShiftReg;
244 unsigned ShiftImm;
245 } ShiftedReg;
Owen Anderson92a20222011-07-21 18:54:16 +0000246 struct {
247 ARM_AM::ShiftOpc ShiftTy;
248 unsigned SrcReg;
249 unsigned ShiftImm;
250 } ShiftedImm;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000251 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000252
Bill Wendling146018f2010-11-06 21:42:12 +0000253 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
254public:
Sean Callanan76264762010-04-02 22:27:05 +0000255 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
256 Kind = o.Kind;
257 StartLoc = o.StartLoc;
258 EndLoc = o.EndLoc;
259 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000260 case CondCode:
261 CC = o.CC;
262 break;
Sean Callanan76264762010-04-02 22:27:05 +0000263 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000264 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000265 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000266 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000267 case Register:
268 Reg = o.Reg;
269 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000270 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000271 case DPRRegisterList:
272 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000273 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000274 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000275 case CoprocNum:
276 case CoprocReg:
277 Cop = o.Cop;
278 break;
Sean Callanan76264762010-04-02 22:27:05 +0000279 case Immediate:
280 Imm = o.Imm;
281 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000282 case MemBarrierOpt:
283 MBOpt = o.MBOpt;
284 break;
Sean Callanan76264762010-04-02 22:27:05 +0000285 case Memory:
286 Mem = o.Mem;
287 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000288 case MSRMask:
289 MMask = o.MMask;
290 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000291 case ProcIFlags:
292 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000293 break;
294 case Shifter:
295 Shift = o.Shift;
296 break;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000297 case ShiftedRegister:
298 ShiftedReg = o.ShiftedReg;
299 break;
Owen Anderson92a20222011-07-21 18:54:16 +0000300 case ShiftedImmediate:
301 ShiftedImm = o.ShiftedImm;
302 break;
Sean Callanan76264762010-04-02 22:27:05 +0000303 }
304 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000305
Sean Callanan76264762010-04-02 22:27:05 +0000306 /// getStartLoc - Get the location of the first token of this operand.
307 SMLoc getStartLoc() const { return StartLoc; }
308 /// getEndLoc - Get the location of the last token of this operand.
309 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000310
Daniel Dunbar8462b302010-08-11 06:36:53 +0000311 ARMCC::CondCodes getCondCode() const {
312 assert(Kind == CondCode && "Invalid access!");
313 return CC.Val;
314 }
315
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000316 unsigned getCoproc() const {
317 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
318 return Cop.Val;
319 }
320
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000321 StringRef getToken() const {
322 assert(Kind == Token && "Invalid access!");
323 return StringRef(Tok.Data, Tok.Length);
324 }
325
326 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000327 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000328 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000329 }
330
Bill Wendling5fa22a12010-11-09 23:28:44 +0000331 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000332 assert((Kind == RegisterList || Kind == DPRRegisterList ||
333 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000334 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000335 }
336
Kevin Enderbycfe07242009-10-13 22:19:02 +0000337 const MCExpr *getImm() const {
338 assert(Kind == Immediate && "Invalid access!");
339 return Imm.Val;
340 }
341
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000342 ARM_MB::MemBOpt getMemBarrierOpt() const {
343 assert(Kind == MemBarrierOpt && "Invalid access!");
344 return MBOpt.Val;
345 }
346
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000347 ARM_PROC::IFlags getProcIFlags() const {
348 assert(Kind == ProcIFlags && "Invalid access!");
349 return IFlags.Val;
350 }
351
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000352 unsigned getMSRMask() const {
353 assert(Kind == MSRMask && "Invalid access!");
354 return MMask.Val;
355 }
356
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000357 /// @name Memory Operand Accessors
358 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000359 ARMII::AddrMode getMemAddrMode() const {
360 return Mem.AddrMode;
361 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000362 unsigned getMemBaseRegNum() const {
363 return Mem.BaseRegNum;
364 }
365 unsigned getMemOffsetRegNum() const {
366 assert(Mem.OffsetIsReg && "Invalid access!");
367 return Mem.Offset.RegNum;
368 }
369 const MCExpr *getMemOffset() const {
370 assert(!Mem.OffsetIsReg && "Invalid access!");
371 return Mem.Offset.Value;
372 }
373 unsigned getMemOffsetRegShifted() const {
374 assert(Mem.OffsetIsReg && "Invalid access!");
375 return Mem.OffsetRegShifted;
376 }
377 const MCExpr *getMemShiftAmount() const {
378 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
379 return Mem.ShiftAmount;
380 }
Owen Anderson00828302011-03-18 22:50:18 +0000381 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000382 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
383 return Mem.ShiftType;
384 }
385 bool getMemPreindexed() const { return Mem.Preindexed; }
386 bool getMemPostindexed() const { return Mem.Postindexed; }
387 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
388 bool getMemNegative() const { return Mem.Negative; }
389 bool getMemWriteback() const { return Mem.Writeback; }
390
391 /// @}
392
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000393 bool isCoprocNum() const { return Kind == CoprocNum; }
394 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000395 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000396 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000397 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000398 bool isImm0_255() const {
399 if (Kind != Immediate)
400 return false;
401 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
402 if (!CE) return false;
403 int64_t Value = CE->getValue();
404 return Value >= 0 && Value < 256;
405 }
Jim Grosbach83ab0702011-07-13 22:01:08 +0000406 bool isImm0_7() const {
407 if (Kind != Immediate)
408 return false;
409 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
410 if (!CE) return false;
411 int64_t Value = CE->getValue();
412 return Value >= 0 && Value < 8;
413 }
414 bool isImm0_15() const {
415 if (Kind != Immediate)
416 return false;
417 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
418 if (!CE) return false;
419 int64_t Value = CE->getValue();
420 return Value >= 0 && Value < 16;
421 }
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000422 bool isImm0_31() const {
423 if (Kind != Immediate)
424 return false;
425 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
426 if (!CE) return false;
427 int64_t Value = CE->getValue();
428 return Value >= 0 && Value < 32;
429 }
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000430 bool isImm0_65535() const {
431 if (Kind != Immediate)
432 return false;
433 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
434 if (!CE) return false;
435 int64_t Value = CE->getValue();
436 return Value >= 0 && Value < 65536;
437 }
Jim Grosbachffa32252011-07-19 19:13:28 +0000438 bool isImm0_65535Expr() const {
439 if (Kind != Immediate)
440 return false;
441 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
442 // If it's not a constant expression, it'll generate a fixup and be
443 // handled later.
444 if (!CE) return true;
445 int64_t Value = CE->getValue();
446 return Value >= 0 && Value < 65536;
447 }
Jim Grosbachf6c05252011-07-21 17:23:04 +0000448 bool isPKHLSLImm() const {
449 if (Kind != Immediate)
450 return false;
451 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
452 if (!CE) return false;
453 int64_t Value = CE->getValue();
454 return Value >= 0 && Value < 32;
455 }
456 bool isPKHASRImm() const {
457 if (Kind != Immediate)
458 return false;
459 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
460 if (!CE) return false;
461 int64_t Value = CE->getValue();
462 return Value > 0 && Value <= 32;
463 }
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000464 bool isARMSOImm() const {
465 if (Kind != Immediate)
466 return false;
467 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
468 if (!CE) return false;
469 int64_t Value = CE->getValue();
470 return ARM_AM::getSOImmVal(Value) != -1;
471 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000472 bool isT2SOImm() const {
473 if (Kind != Immediate)
474 return false;
475 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
476 if (!CE) return false;
477 int64_t Value = CE->getValue();
478 return ARM_AM::getT2SOImmVal(Value) != -1;
479 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000480 bool isSetEndImm() const {
481 if (Kind != Immediate)
482 return false;
483 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
484 if (!CE) return false;
485 int64_t Value = CE->getValue();
486 return Value == 1 || Value == 0;
487 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000488 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000489 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000490 bool isDPRRegList() const { return Kind == DPRRegisterList; }
491 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000492 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000493 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000494 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000495 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000496 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Owen Anderson92a20222011-07-21 18:54:16 +0000497 bool isShiftedImm() const { return Kind == ShiftedImmediate; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000498 bool isMemMode2() const {
499 if (getMemAddrMode() != ARMII::AddrMode2)
500 return false;
501
502 if (getMemOffsetIsReg())
503 return true;
504
505 if (getMemNegative() &&
506 !(getMemPostindexed() || getMemPreindexed()))
507 return false;
508
509 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
510 if (!CE) return false;
511 int64_t Value = CE->getValue();
512
513 // The offset must be in the range 0-4095 (imm12).
514 if (Value > 4095 || Value < -4095)
515 return false;
516
517 return true;
518 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000519 bool isMemMode3() const {
520 if (getMemAddrMode() != ARMII::AddrMode3)
521 return false;
522
523 if (getMemOffsetIsReg()) {
524 if (getMemOffsetRegShifted())
525 return false; // No shift with offset reg allowed
526 return true;
527 }
528
529 if (getMemNegative() &&
530 !(getMemPostindexed() || getMemPreindexed()))
531 return false;
532
533 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
534 if (!CE) return false;
535 int64_t Value = CE->getValue();
536
537 // The offset must be in the range 0-255 (imm8).
538 if (Value > 255 || Value < -255)
539 return false;
540
541 return true;
542 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000543 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000544 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
545 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000546 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000547
Daniel Dunbar4b462672011-01-18 05:55:27 +0000548 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000549 if (!CE) return false;
550
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000551 // The offset must be a multiple of 4 in the range 0-1020.
552 int64_t Value = CE->getValue();
553 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
554 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000555 bool isMemMode7() const {
556 if (!isMemory() ||
557 getMemPreindexed() ||
558 getMemPostindexed() ||
559 getMemOffsetIsReg() ||
560 getMemNegative() ||
561 getMemWriteback())
562 return false;
563
564 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
565 if (!CE) return false;
566
567 if (CE->getValue())
568 return false;
569
570 return true;
571 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000572 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000573 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000574 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000575 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000576 }
577 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000578 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000579 return false;
580
Daniel Dunbar4b462672011-01-18 05:55:27 +0000581 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000582 if (!CE) return false;
583
584 // The offset must be a multiple of 4 in the range 0-124.
585 uint64_t Value = CE->getValue();
586 return ((Value & 0x3) == 0 && Value <= 124);
587 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000588 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000589 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000590
591 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000592 // Add as immediates when possible. Null MCExpr = 0.
593 if (Expr == 0)
594 Inst.addOperand(MCOperand::CreateImm(0));
595 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000596 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
597 else
598 Inst.addOperand(MCOperand::CreateExpr(Expr));
599 }
600
Daniel Dunbar8462b302010-08-11 06:36:53 +0000601 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000602 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000603 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000604 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
605 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000606 }
607
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000608 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
609 assert(N == 1 && "Invalid number of operands!");
610 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
611 }
612
613 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
614 assert(N == 1 && "Invalid number of operands!");
615 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
616 }
617
Jim Grosbachd67641b2010-12-06 18:21:12 +0000618 void addCCOutOperands(MCInst &Inst, unsigned N) const {
619 assert(N == 1 && "Invalid number of operands!");
620 Inst.addOperand(MCOperand::CreateReg(getReg()));
621 }
622
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000623 void addRegOperands(MCInst &Inst, unsigned N) const {
624 assert(N == 1 && "Invalid number of operands!");
625 Inst.addOperand(MCOperand::CreateReg(getReg()));
626 }
627
Jim Grosbache8606dc2011-07-13 17:50:29 +0000628 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
629 assert(N == 3 && "Invalid number of operands!");
630 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
Jim Grosbache8606dc2011-07-13 17:50:29 +0000631 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
632 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
633 Inst.addOperand(MCOperand::CreateImm(
634 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
635 }
636
Owen Anderson92a20222011-07-21 18:54:16 +0000637 void addShiftedImmOperands(MCInst &Inst, unsigned N) const {
Owen Anderson152d4a42011-07-21 23:38:37 +0000638 assert(N == 2 && "Invalid number of operands!");
Owen Anderson92a20222011-07-21 18:54:16 +0000639 assert(isShiftedImm() && "addShiftedImmOperands() on non ShiftedImm!");
640 Inst.addOperand(MCOperand::CreateReg(ShiftedImm.SrcReg));
Owen Anderson92a20222011-07-21 18:54:16 +0000641 Inst.addOperand(MCOperand::CreateImm(
642 ARM_AM::getSORegOpc(ShiftedImm.ShiftTy, ShiftedImm.ShiftImm)));
643 }
644
645
Owen Anderson00828302011-03-18 22:50:18 +0000646 void addShifterOperands(MCInst &Inst, unsigned N) const {
647 assert(N == 1 && "Invalid number of operands!");
648 Inst.addOperand(MCOperand::CreateImm(
649 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
650 }
651
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000652 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000653 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000654 const SmallVectorImpl<unsigned> &RegList = getRegList();
655 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000656 I = RegList.begin(), E = RegList.end(); I != E; ++I)
657 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000658 }
659
Bill Wendling0f630752010-11-17 04:32:08 +0000660 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
661 addRegListOperands(Inst, N);
662 }
663
664 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
665 addRegListOperands(Inst, N);
666 }
667
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000668 void addImmOperands(MCInst &Inst, unsigned N) const {
669 assert(N == 1 && "Invalid number of operands!");
670 addExpr(Inst, getImm());
671 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000672
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000673 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
674 assert(N == 1 && "Invalid number of operands!");
675 addExpr(Inst, getImm());
676 }
677
Jim Grosbach83ab0702011-07-13 22:01:08 +0000678 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
679 assert(N == 1 && "Invalid number of operands!");
680 addExpr(Inst, getImm());
681 }
682
683 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
684 assert(N == 1 && "Invalid number of operands!");
685 addExpr(Inst, getImm());
686 }
687
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000688 void addImm0_31Operands(MCInst &Inst, unsigned N) const {
689 assert(N == 1 && "Invalid number of operands!");
690 addExpr(Inst, getImm());
691 }
692
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000693 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
694 assert(N == 1 && "Invalid number of operands!");
695 addExpr(Inst, getImm());
696 }
697
Jim Grosbachffa32252011-07-19 19:13:28 +0000698 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
699 assert(N == 1 && "Invalid number of operands!");
700 addExpr(Inst, getImm());
701 }
702
Jim Grosbachf6c05252011-07-21 17:23:04 +0000703 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
704 assert(N == 1 && "Invalid number of operands!");
705 addExpr(Inst, getImm());
706 }
707
708 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
709 assert(N == 1 && "Invalid number of operands!");
710 // An ASR value of 32 encodes as 0, so that's how we want to add it to
711 // the instruction as well.
712 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
713 int Val = CE->getValue();
714 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
715 }
716
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000717 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
718 assert(N == 1 && "Invalid number of operands!");
719 addExpr(Inst, getImm());
720 }
721
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000722 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
723 assert(N == 1 && "Invalid number of operands!");
724 addExpr(Inst, getImm());
725 }
726
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000727 void addSetEndImmOperands(MCInst &Inst, unsigned N) const {
728 assert(N == 1 && "Invalid number of operands!");
729 addExpr(Inst, getImm());
730 }
731
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000732 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
733 assert(N == 1 && "Invalid number of operands!");
734 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
735 }
736
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000737 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
738 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
739 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
740
741 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000742 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000743 assert((CE || CE->getValue() == 0) &&
744 "No offset operand support in mode 7");
745 }
746
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000747 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
748 assert(isMemMode2() && "Invalid mode or number of operands!");
749 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
750 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
751
752 if (getMemOffsetIsReg()) {
753 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
754
755 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
756 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
757 int64_t ShiftAmount = 0;
758
759 if (getMemOffsetRegShifted()) {
760 ShOpc = getMemShiftType();
761 const MCConstantExpr *CE =
762 dyn_cast<MCConstantExpr>(getMemShiftAmount());
763 ShiftAmount = CE->getValue();
764 }
765
766 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
767 ShOpc, IdxMode)));
768 return;
769 }
770
771 // Create a operand placeholder to always yield the same number of operands.
772 Inst.addOperand(MCOperand::CreateReg(0));
773
774 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
775 // the difference?
776 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
777 assert(CE && "Non-constant mode 2 offset operand!");
778 int64_t Offset = CE->getValue();
779
780 if (Offset >= 0)
781 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
782 Offset, ARM_AM::no_shift, IdxMode)));
783 else
784 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
785 -Offset, ARM_AM::no_shift, IdxMode)));
786 }
787
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000788 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
789 assert(isMemMode3() && "Invalid mode or number of operands!");
790 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
791 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
792
793 if (getMemOffsetIsReg()) {
794 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
795
796 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
797 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
798 IdxMode)));
799 return;
800 }
801
802 // Create a operand placeholder to always yield the same number of operands.
803 Inst.addOperand(MCOperand::CreateReg(0));
804
805 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
806 // the difference?
807 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
808 assert(CE && "Non-constant mode 3 offset operand!");
809 int64_t Offset = CE->getValue();
810
811 if (Offset >= 0)
812 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
813 Offset, IdxMode)));
814 else
815 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
816 -Offset, IdxMode)));
817 }
818
Chris Lattner14b93852010-10-29 00:27:31 +0000819 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
820 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000821
Daniel Dunbar4b462672011-01-18 05:55:27 +0000822 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
823 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000824
Jim Grosbach80eb2332010-10-29 17:41:25 +0000825 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
826 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000827 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000828 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000829
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000830 // The MCInst offset operand doesn't include the low two bits (like
831 // the instruction encoding).
832 int64_t Offset = CE->getValue() / 4;
833 if (Offset >= 0)
834 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
835 Offset)));
836 else
837 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
838 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000839 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000840
Bill Wendlingf4caf692010-12-14 03:36:38 +0000841 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
842 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000843 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
844 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000845 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000846
Bill Wendlingf4caf692010-12-14 03:36:38 +0000847 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
848 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000849 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
850 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000851 assert(CE && "Non-constant mode offset operand!");
852 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000853 }
854
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000855 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
856 assert(N == 1 && "Invalid number of operands!");
857 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
858 }
859
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000860 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
861 assert(N == 1 && "Invalid number of operands!");
862 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
863 }
864
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000865 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000866
Chris Lattner3a697562010-10-28 17:20:03 +0000867 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
868 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000869 Op->CC.Val = CC;
870 Op->StartLoc = S;
871 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000872 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000873 }
874
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000875 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
876 ARMOperand *Op = new ARMOperand(CoprocNum);
877 Op->Cop.Val = CopVal;
878 Op->StartLoc = S;
879 Op->EndLoc = S;
880 return Op;
881 }
882
883 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
884 ARMOperand *Op = new ARMOperand(CoprocReg);
885 Op->Cop.Val = CopVal;
886 Op->StartLoc = S;
887 Op->EndLoc = S;
888 return Op;
889 }
890
Jim Grosbachd67641b2010-12-06 18:21:12 +0000891 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
892 ARMOperand *Op = new ARMOperand(CCOut);
893 Op->Reg.RegNum = RegNum;
894 Op->StartLoc = S;
895 Op->EndLoc = S;
896 return Op;
897 }
898
Chris Lattner3a697562010-10-28 17:20:03 +0000899 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
900 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000901 Op->Tok.Data = Str.data();
902 Op->Tok.Length = Str.size();
903 Op->StartLoc = S;
904 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000905 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000906 }
907
Bill Wendling50d0f582010-11-18 23:43:05 +0000908 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000909 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000910 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000911 Op->StartLoc = S;
912 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000913 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000914 }
915
Jim Grosbache8606dc2011-07-13 17:50:29 +0000916 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
917 unsigned SrcReg,
918 unsigned ShiftReg,
919 unsigned ShiftImm,
920 SMLoc S, SMLoc E) {
921 ARMOperand *Op = new ARMOperand(ShiftedRegister);
922 Op->ShiftedReg.ShiftTy = ShTy;
923 Op->ShiftedReg.SrcReg = SrcReg;
924 Op->ShiftedReg.ShiftReg = ShiftReg;
925 Op->ShiftedReg.ShiftImm = ShiftImm;
926 Op->StartLoc = S;
927 Op->EndLoc = E;
928 return Op;
929 }
930
Owen Anderson92a20222011-07-21 18:54:16 +0000931 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
932 unsigned SrcReg,
933 unsigned ShiftImm,
934 SMLoc S, SMLoc E) {
935 ARMOperand *Op = new ARMOperand(ShiftedImmediate);
936 Op->ShiftedImm.ShiftTy = ShTy;
937 Op->ShiftedImm.SrcReg = SrcReg;
938 Op->ShiftedImm.ShiftImm = ShiftImm;
939 Op->StartLoc = S;
940 Op->EndLoc = E;
941 return Op;
942 }
943
Owen Anderson00828302011-03-18 22:50:18 +0000944 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
945 SMLoc S, SMLoc E) {
946 ARMOperand *Op = new ARMOperand(Shifter);
947 Op->Shift.ShiftTy = ShTy;
948 Op->StartLoc = S;
949 Op->EndLoc = E;
950 return Op;
951 }
952
Bill Wendling7729e062010-11-09 22:44:22 +0000953 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000954 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000955 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000956 KindTy Kind = RegisterList;
957
958 if (ARM::DPRRegClass.contains(Regs.front().first))
959 Kind = DPRRegisterList;
960 else if (ARM::SPRRegClass.contains(Regs.front().first))
961 Kind = SPRRegisterList;
962
963 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000964 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000965 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000966 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000967 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000968 Op->StartLoc = StartLoc;
969 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000970 return Op;
971 }
972
Chris Lattner3a697562010-10-28 17:20:03 +0000973 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
974 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000975 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000976 Op->StartLoc = S;
977 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000978 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000979 }
980
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000981 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
982 bool OffsetIsReg, const MCExpr *Offset,
983 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000984 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000985 const MCExpr *ShiftAmount, bool Preindexed,
986 bool Postindexed, bool Negative, bool Writeback,
987 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000988 assert((OffsetRegNum == -1 || OffsetIsReg) &&
989 "OffsetRegNum must imply OffsetIsReg!");
990 assert((!OffsetRegShifted || OffsetIsReg) &&
991 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000992 assert((Offset || OffsetIsReg) &&
993 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000994 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
995 "Cannot have shift amount without shifted register offset!");
996 assert((!Offset || !OffsetIsReg) &&
997 "Cannot have expression offset and register offset!");
998
Chris Lattner3a697562010-10-28 17:20:03 +0000999 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001000 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +00001001 Op->Mem.BaseRegNum = BaseRegNum;
1002 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +00001003 if (OffsetIsReg)
1004 Op->Mem.Offset.RegNum = OffsetRegNum;
1005 else
1006 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +00001007 Op->Mem.OffsetRegShifted = OffsetRegShifted;
1008 Op->Mem.ShiftType = ShiftType;
1009 Op->Mem.ShiftAmount = ShiftAmount;
1010 Op->Mem.Preindexed = Preindexed;
1011 Op->Mem.Postindexed = Postindexed;
1012 Op->Mem.Negative = Negative;
1013 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +00001014
Sean Callanan76264762010-04-02 22:27:05 +00001015 Op->StartLoc = S;
1016 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001017 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001018 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001019
1020 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
1021 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
1022 Op->MBOpt.Val = Opt;
1023 Op->StartLoc = S;
1024 Op->EndLoc = S;
1025 return Op;
1026 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001027
1028 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1029 ARMOperand *Op = new ARMOperand(ProcIFlags);
1030 Op->IFlags.Val = IFlags;
1031 Op->StartLoc = S;
1032 Op->EndLoc = S;
1033 return Op;
1034 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001035
1036 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1037 ARMOperand *Op = new ARMOperand(MSRMask);
1038 Op->MMask.Val = MMask;
1039 Op->StartLoc = S;
1040 Op->EndLoc = S;
1041 return Op;
1042 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001043};
1044
1045} // end anonymous namespace.
1046
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001047void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001048 switch (Kind) {
1049 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +00001050 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001051 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +00001052 case CCOut:
1053 OS << "<ccout " << getReg() << ">";
1054 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001055 case CoprocNum:
1056 OS << "<coprocessor number: " << getCoproc() << ">";
1057 break;
1058 case CoprocReg:
1059 OS << "<coprocessor register: " << getCoproc() << ">";
1060 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001061 case MSRMask:
1062 OS << "<mask: " << getMSRMask() << ">";
1063 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001064 case Immediate:
1065 getImm()->print(OS);
1066 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001067 case MemBarrierOpt:
1068 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1069 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001070 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001071 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001072 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
1073 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001074 if (getMemOffsetIsReg()) {
1075 OS << " offset:<register " << getMemOffsetRegNum();
1076 if (getMemOffsetRegShifted()) {
1077 OS << " offset-shift-type:" << getMemShiftType();
1078 OS << " offset-shift-amount:" << *getMemShiftAmount();
1079 }
1080 } else {
1081 OS << " offset:" << *getMemOffset();
1082 }
1083 if (getMemOffsetIsReg())
1084 OS << " (offset-is-reg)";
1085 if (getMemPreindexed())
1086 OS << " (pre-indexed)";
1087 if (getMemPostindexed())
1088 OS << " (post-indexed)";
1089 if (getMemNegative())
1090 OS << " (negative)";
1091 if (getMemWriteback())
1092 OS << " (writeback)";
1093 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001094 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001095 case ProcIFlags: {
1096 OS << "<ARM_PROC::";
1097 unsigned IFlags = getProcIFlags();
1098 for (int i=2; i >= 0; --i)
1099 if (IFlags & (1 << i))
1100 OS << ARM_PROC::IFlagsToString(1 << i);
1101 OS << ">";
1102 break;
1103 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001104 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +00001105 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001106 break;
Owen Anderson00828302011-03-18 22:50:18 +00001107 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +00001108 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
1109 break;
1110 case ShiftedRegister:
Owen Anderson92a20222011-07-21 18:54:16 +00001111 OS << "<so_reg_reg "
Jim Grosbache8606dc2011-07-13 17:50:29 +00001112 << ShiftedReg.SrcReg
1113 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
1114 << ", " << ShiftedReg.ShiftReg << ", "
1115 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
1116 << ">";
Owen Anderson00828302011-03-18 22:50:18 +00001117 break;
Owen Anderson92a20222011-07-21 18:54:16 +00001118 case ShiftedImmediate:
1119 OS << "<so_reg_imm "
1120 << ShiftedImm.SrcReg
1121 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedImm.ShiftImm))
1122 << ", " << ARM_AM::getSORegOffset(ShiftedImm.ShiftImm)
1123 << ">";
1124 break;
Bill Wendling0f630752010-11-17 04:32:08 +00001125 case RegisterList:
1126 case DPRRegisterList:
1127 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00001128 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001129
Bill Wendling5fa22a12010-11-09 23:28:44 +00001130 const SmallVectorImpl<unsigned> &RegList = getRegList();
1131 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001132 I = RegList.begin(), E = RegList.end(); I != E; ) {
1133 OS << *I;
1134 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001135 }
1136
1137 OS << ">";
1138 break;
1139 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001140 case Token:
1141 OS << "'" << getToken() << "'";
1142 break;
1143 }
1144}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001145
1146/// @name Auto-generated Match Functions
1147/// {
1148
1149static unsigned MatchRegisterName(StringRef Name);
1150
1151/// }
1152
Bob Wilson69df7232011-02-03 21:46:10 +00001153bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1154 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +00001155 RegNo = TryParseRegister();
1156
1157 return (RegNo == (unsigned)-1);
1158}
1159
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001160/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00001161/// and if it is a register name the token is eaten and the register number is
1162/// returned. Otherwise return -1.
1163///
1164int ARMAsmParser::TryParseRegister() {
1165 const AsmToken &Tok = Parser.getTok();
1166 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001167
Chris Lattnere5658fa2010-10-30 04:09:10 +00001168 // FIXME: Validate register for the current architecture; we have to do
1169 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001170 std::string upperCase = Tok.getString().str();
1171 std::string lowerCase = LowercaseString(upperCase);
1172 unsigned RegNum = MatchRegisterName(lowerCase);
1173 if (!RegNum) {
1174 RegNum = StringSwitch<unsigned>(lowerCase)
1175 .Case("r13", ARM::SP)
1176 .Case("r14", ARM::LR)
1177 .Case("r15", ARM::PC)
1178 .Case("ip", ARM::R12)
1179 .Default(0);
1180 }
1181 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001182
Chris Lattnere5658fa2010-10-30 04:09:10 +00001183 Parser.Lex(); // Eat identifier token.
1184 return RegNum;
1185}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001186
Jim Grosbach19906722011-07-13 18:49:30 +00001187// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1188// If a recoverable error occurs, return 1. If an irrecoverable error
1189// occurs, return -1. An irrecoverable error is one where tokens have been
1190// consumed in the process of trying to parse the shifter (i.e., when it is
1191// indeed a shifter operand, but malformed).
1192int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001193 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1194 SMLoc S = Parser.getTok().getLoc();
1195 const AsmToken &Tok = Parser.getTok();
1196 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1197
1198 std::string upperCase = Tok.getString().str();
1199 std::string lowerCase = LowercaseString(upperCase);
1200 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1201 .Case("lsl", ARM_AM::lsl)
1202 .Case("lsr", ARM_AM::lsr)
1203 .Case("asr", ARM_AM::asr)
1204 .Case("ror", ARM_AM::ror)
1205 .Case("rrx", ARM_AM::rrx)
1206 .Default(ARM_AM::no_shift);
1207
1208 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001209 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001210
Jim Grosbache8606dc2011-07-13 17:50:29 +00001211 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001212
Jim Grosbache8606dc2011-07-13 17:50:29 +00001213 // The source register for the shift has already been added to the
1214 // operand list, so we need to pop it off and combine it into the shifted
1215 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00001216 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00001217 if (!PrevOp->isReg())
1218 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1219 int SrcReg = PrevOp->getReg();
1220 int64_t Imm = 0;
1221 int ShiftReg = 0;
1222 if (ShiftTy == ARM_AM::rrx) {
1223 // RRX Doesn't have an explicit shift amount. The encoder expects
1224 // the shift register to be the same as the source register. Seems odd,
1225 // but OK.
1226 ShiftReg = SrcReg;
1227 } else {
1228 // Figure out if this is shifted by a constant or a register (for non-RRX).
1229 if (Parser.getTok().is(AsmToken::Hash)) {
1230 Parser.Lex(); // Eat hash.
1231 SMLoc ImmLoc = Parser.getTok().getLoc();
1232 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001233 if (getParser().ParseExpression(ShiftExpr)) {
1234 Error(ImmLoc, "invalid immediate shift value");
1235 return -1;
1236 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001237 // The expression must be evaluatable as an immediate.
1238 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001239 if (!CE) {
1240 Error(ImmLoc, "invalid immediate shift value");
1241 return -1;
1242 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001243 // Range check the immediate.
1244 // lsl, ror: 0 <= imm <= 31
1245 // lsr, asr: 0 <= imm <= 32
1246 Imm = CE->getValue();
1247 if (Imm < 0 ||
1248 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1249 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001250 Error(ImmLoc, "immediate shift value out of range");
1251 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001252 }
1253 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1254 ShiftReg = TryParseRegister();
1255 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001256 if (ShiftReg == -1) {
1257 Error (L, "expected immediate or register in shift operand");
1258 return -1;
1259 }
1260 } else {
1261 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001262 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001263 return -1;
1264 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001265 }
1266
Owen Anderson92a20222011-07-21 18:54:16 +00001267 if (ShiftReg && ShiftTy != ARM_AM::rrx)
1268 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
Jim Grosbache8606dc2011-07-13 17:50:29 +00001269 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001270 S, Parser.getTok().getLoc()));
Owen Anderson92a20222011-07-21 18:54:16 +00001271 else
1272 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
1273 S, Parser.getTok().getLoc()));
Owen Anderson00828302011-03-18 22:50:18 +00001274
Jim Grosbach19906722011-07-13 18:49:30 +00001275 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001276}
1277
1278
Bill Wendling50d0f582010-11-18 23:43:05 +00001279/// Try to parse a register name. The token must be an Identifier when called.
1280/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1281/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001282///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001283/// TODO this is likely to change to allow different register types and or to
1284/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001285bool ARMAsmParser::
1286TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001287 SMLoc S = Parser.getTok().getLoc();
1288 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001289 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001290 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001291
Bill Wendling50d0f582010-11-18 23:43:05 +00001292 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001293
Chris Lattnere5658fa2010-10-30 04:09:10 +00001294 const AsmToken &ExclaimTok = Parser.getTok();
1295 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001296 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1297 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001298 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001299 }
1300
Bill Wendling50d0f582010-11-18 23:43:05 +00001301 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001302}
1303
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001304/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1305/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1306/// "c5", ...
1307static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001308 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1309 // but efficient.
1310 switch (Name.size()) {
1311 default: break;
1312 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001313 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001314 return -1;
1315 switch (Name[1]) {
1316 default: return -1;
1317 case '0': return 0;
1318 case '1': return 1;
1319 case '2': return 2;
1320 case '3': return 3;
1321 case '4': return 4;
1322 case '5': return 5;
1323 case '6': return 6;
1324 case '7': return 7;
1325 case '8': return 8;
1326 case '9': return 9;
1327 }
1328 break;
1329 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001330 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001331 return -1;
1332 switch (Name[2]) {
1333 default: return -1;
1334 case '0': return 10;
1335 case '1': return 11;
1336 case '2': return 12;
1337 case '3': return 13;
1338 case '4': return 14;
1339 case '5': return 15;
1340 }
1341 break;
1342 }
1343
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001344 return -1;
1345}
1346
Jim Grosbachf922c472011-02-12 01:34:40 +00001347/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001348/// token must be an Identifier when called, and if it is a coprocessor
1349/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001350ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1351tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001352 SMLoc S = Parser.getTok().getLoc();
1353 const AsmToken &Tok = Parser.getTok();
1354 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1355
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001356 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001357 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001358 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001359
1360 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001361 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001362 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001363}
1364
Jim Grosbachf922c472011-02-12 01:34:40 +00001365/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001366/// token must be an Identifier when called, and if it is a coprocessor
1367/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001368ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1369tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001370 SMLoc S = Parser.getTok().getLoc();
1371 const AsmToken &Tok = Parser.getTok();
1372 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1373
1374 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1375 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001376 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001377
1378 Parser.Lex(); // Eat identifier token.
1379 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001380 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001381}
1382
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001383/// Parse a register list, return it if successful else return null. The first
1384/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001385bool ARMAsmParser::
1386ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001387 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001388 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001389 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001390
Bill Wendling7729e062010-11-09 22:44:22 +00001391 // Read the rest of the registers in the list.
1392 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001393 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001394
Bill Wendling7729e062010-11-09 22:44:22 +00001395 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001396 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001397 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001398
Sean Callanan18b83232010-01-19 21:44:56 +00001399 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001400 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001401 if (RegTok.isNot(AsmToken::Identifier)) {
1402 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001403 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001404 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001405
Bill Wendling1d6a2652010-11-06 10:40:24 +00001406 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001407 if (RegNum == -1) {
1408 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001409 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001410 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001411
Bill Wendlinge7176102010-11-06 22:36:58 +00001412 if (IsRange) {
1413 int Reg = PrevRegNum;
1414 do {
1415 ++Reg;
1416 Registers.push_back(std::make_pair(Reg, RegLoc));
1417 } while (Reg != RegNum);
1418 } else {
1419 Registers.push_back(std::make_pair(RegNum, RegLoc));
1420 }
1421
1422 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001423 } while (Parser.getTok().is(AsmToken::Comma) ||
1424 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001425
1426 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001427 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001428 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1429 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001430 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001431 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001432
Bill Wendlinge7176102010-11-06 22:36:58 +00001433 SMLoc E = RCurlyTok.getLoc();
1434 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001435
Bill Wendlinge7176102010-11-06 22:36:58 +00001436 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001437 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001438 RI = Registers.begin(), RE = Registers.end();
1439
Bill Wendling7caebff2011-01-12 21:20:59 +00001440 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001441 bool EmittedWarning = false;
1442
Bill Wendling7caebff2011-01-12 21:20:59 +00001443 DenseMap<unsigned, bool> RegMap;
1444 RegMap[HighRegNum] = true;
1445
Bill Wendlinge7176102010-11-06 22:36:58 +00001446 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001447 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001448 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001449
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001450 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001451 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001452 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001453 }
1454
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001455 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001456 Warning(RegInfo.second,
1457 "register not in ascending order in register list");
1458
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001459 RegMap[Reg] = true;
1460 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001461 }
1462
Bill Wendling50d0f582010-11-18 23:43:05 +00001463 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1464 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001465}
1466
Jim Grosbachf922c472011-02-12 01:34:40 +00001467/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1468ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1469tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001470 SMLoc S = Parser.getTok().getLoc();
1471 const AsmToken &Tok = Parser.getTok();
1472 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1473 StringRef OptStr = Tok.getString();
1474
1475 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1476 .Case("sy", ARM_MB::SY)
1477 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001478 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001479 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001480 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001481 .Case("ishst", ARM_MB::ISHST)
1482 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001483 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001484 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001485 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001486 .Case("osh", ARM_MB::OSH)
1487 .Case("oshst", ARM_MB::OSHST)
1488 .Default(~0U);
1489
1490 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001491 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001492
1493 Parser.Lex(); // Eat identifier token.
1494 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001495 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001496}
1497
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001498/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001499ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1500tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1501 SMLoc S = Parser.getTok().getLoc();
1502 const AsmToken &Tok = Parser.getTok();
1503 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1504 StringRef IFlagsStr = Tok.getString();
1505
1506 unsigned IFlags = 0;
1507 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1508 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1509 .Case("a", ARM_PROC::A)
1510 .Case("i", ARM_PROC::I)
1511 .Case("f", ARM_PROC::F)
1512 .Default(~0U);
1513
1514 // If some specific iflag is already set, it means that some letter is
1515 // present more than once, this is not acceptable.
1516 if (Flag == ~0U || (IFlags & Flag))
1517 return MatchOperand_NoMatch;
1518
1519 IFlags |= Flag;
1520 }
1521
1522 Parser.Lex(); // Eat identifier token.
1523 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1524 return MatchOperand_Success;
1525}
1526
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001527/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1528ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1529tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1530 SMLoc S = Parser.getTok().getLoc();
1531 const AsmToken &Tok = Parser.getTok();
1532 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1533 StringRef Mask = Tok.getString();
1534
1535 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1536 size_t Start = 0, Next = Mask.find('_');
1537 StringRef Flags = "";
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00001538 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001539 if (Next != StringRef::npos)
1540 Flags = Mask.slice(Next+1, Mask.size());
1541
1542 // FlagsVal contains the complete mask:
1543 // 3-0: Mask
1544 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1545 unsigned FlagsVal = 0;
1546
1547 if (SpecReg == "apsr") {
1548 FlagsVal = StringSwitch<unsigned>(Flags)
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00001549 .Case("nzcvq", 0x8) // same as CPSR_f
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001550 .Case("g", 0x4) // same as CPSR_s
1551 .Case("nzcvqg", 0xc) // same as CPSR_fs
1552 .Default(~0U);
1553
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001554 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001555 if (!Flags.empty())
1556 return MatchOperand_NoMatch;
1557 else
1558 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001559 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001560 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001561 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1562 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001563 for (int i = 0, e = Flags.size(); i != e; ++i) {
1564 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1565 .Case("c", 1)
1566 .Case("x", 2)
1567 .Case("s", 4)
1568 .Case("f", 8)
1569 .Default(~0U);
1570
1571 // If some specific flag is already set, it means that some letter is
1572 // present more than once, this is not acceptable.
1573 if (FlagsVal == ~0U || (FlagsVal & Flag))
1574 return MatchOperand_NoMatch;
1575 FlagsVal |= Flag;
1576 }
1577 } else // No match for special register.
1578 return MatchOperand_NoMatch;
1579
1580 // Special register without flags are equivalent to "fc" flags.
1581 if (!FlagsVal)
1582 FlagsVal = 0x9;
1583
1584 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1585 if (SpecReg == "spsr")
1586 FlagsVal |= 16;
1587
1588 Parser.Lex(); // Eat identifier token.
1589 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1590 return MatchOperand_Success;
1591}
1592
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001593/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1594ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1595tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001596 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001597
1598 if (ParseMemory(Operands, ARMII::AddrMode2))
1599 return MatchOperand_NoMatch;
1600
1601 return MatchOperand_Success;
1602}
1603
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001604/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1605ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1606tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1607 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1608
1609 if (ParseMemory(Operands, ARMII::AddrMode3))
1610 return MatchOperand_NoMatch;
1611
1612 return MatchOperand_Success;
1613}
1614
Jim Grosbachf6c05252011-07-21 17:23:04 +00001615ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1616parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
1617 int Low, int High) {
1618 const AsmToken &Tok = Parser.getTok();
1619 if (Tok.isNot(AsmToken::Identifier)) {
1620 Error(Parser.getTok().getLoc(), Op + " operand expected.");
1621 return MatchOperand_ParseFail;
1622 }
1623 StringRef ShiftName = Tok.getString();
1624 std::string LowerOp = LowercaseString(Op);
1625 std::string UpperOp = UppercaseString(Op);
1626 if (ShiftName != LowerOp && ShiftName != UpperOp) {
1627 Error(Parser.getTok().getLoc(), Op + " operand expected.");
1628 return MatchOperand_ParseFail;
1629 }
1630 Parser.Lex(); // Eat shift type token.
1631
1632 // There must be a '#' and a shift amount.
1633 if (Parser.getTok().isNot(AsmToken::Hash)) {
1634 Error(Parser.getTok().getLoc(), "'#' expected");
1635 return MatchOperand_ParseFail;
1636 }
1637 Parser.Lex(); // Eat hash token.
1638
1639 const MCExpr *ShiftAmount;
1640 SMLoc Loc = Parser.getTok().getLoc();
1641 if (getParser().ParseExpression(ShiftAmount)) {
1642 Error(Loc, "illegal expression");
1643 return MatchOperand_ParseFail;
1644 }
1645 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
1646 if (!CE) {
1647 Error(Loc, "constant expression expected");
1648 return MatchOperand_ParseFail;
1649 }
1650 int Val = CE->getValue();
1651 if (Val < Low || Val > High) {
1652 Error(Loc, "immediate value out of range");
1653 return MatchOperand_ParseFail;
1654 }
1655
1656 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
1657
1658 return MatchOperand_Success;
1659}
1660
Jim Grosbachc27d4f92011-07-22 17:44:50 +00001661ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1662parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1663 const AsmToken &Tok = Parser.getTok();
1664 SMLoc S = Tok.getLoc();
1665 if (Tok.isNot(AsmToken::Identifier)) {
1666 Error(Tok.getLoc(), "'be' or 'le' operand expected");
1667 return MatchOperand_ParseFail;
1668 }
1669 int Val = StringSwitch<int>(Tok.getString())
1670 .Case("be", 1)
1671 .Case("le", 0)
1672 .Default(-1);
1673 Parser.Lex(); // Eat the token.
1674
1675 if (Val == -1) {
1676 Error(Tok.getLoc(), "'be' or 'le' operand expected");
1677 return MatchOperand_ParseFail;
1678 }
1679 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
1680 getContext()),
1681 S, Parser.getTok().getLoc()));
1682 return MatchOperand_Success;
1683}
1684
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001685/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1686/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1687/// when they refer multiple MIOperands inside a single one.
1688bool ARMAsmParser::
1689CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1690 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1691 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1692
1693 // Create a writeback register dummy placeholder.
1694 Inst.addOperand(MCOperand::CreateImm(0));
1695
1696 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1697 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1698 return true;
1699}
1700
1701/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1702/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1703/// when they refer multiple MIOperands inside a single one.
1704bool ARMAsmParser::
1705CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1706 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1707 // Create a writeback register dummy placeholder.
1708 Inst.addOperand(MCOperand::CreateImm(0));
1709 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1710 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1711 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1712 return true;
1713}
1714
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001715/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1716/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1717/// when they refer multiple MIOperands inside a single one.
1718bool ARMAsmParser::
1719CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1720 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1721 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1722
1723 // Create a writeback register dummy placeholder.
1724 Inst.addOperand(MCOperand::CreateImm(0));
1725
1726 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1727 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1728 return true;
1729}
1730
1731/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1732/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1733/// when they refer multiple MIOperands inside a single one.
1734bool ARMAsmParser::
1735CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1736 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1737 // Create a writeback register dummy placeholder.
1738 Inst.addOperand(MCOperand::CreateImm(0));
1739 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1740 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1741 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1742 return true;
1743}
1744
Bill Wendlinge7176102010-11-06 22:36:58 +00001745/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001746/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001747///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001748/// TODO Only preindexing and postindexing addressing are started, unindexed
1749/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001750bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001751ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1752 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001753 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001754 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001755 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001756 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001757 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001758
Sean Callanan18b83232010-01-19 21:44:56 +00001759 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001760 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1761 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001762 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001763 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001764 int BaseRegNum = TryParseRegister();
1765 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001766 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001767 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001768 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001769
Daniel Dunbar05710932011-01-18 05:34:17 +00001770 // The next token must either be a comma or a closing bracket.
1771 const AsmToken &Tok = Parser.getTok();
1772 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1773 return true;
1774
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001775 bool Preindexed = false;
1776 bool Postindexed = false;
1777 bool OffsetIsReg = false;
1778 bool Negative = false;
1779 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001780 ARMOperand *WBOp = 0;
1781 int OffsetRegNum = -1;
1782 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001783 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001784 const MCExpr *ShiftAmount = 0;
1785 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001786
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001787 // First look for preindexed address forms, that is after the "[Rn" we now
1788 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001789 if (Tok.is(AsmToken::Comma)) {
1790 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001791 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001792
Chris Lattner550276e2010-10-28 20:52:15 +00001793 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1794 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001795 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001796 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001797 if (RBracTok.isNot(AsmToken::RBrac)) {
1798 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001799 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001800 }
Sean Callanan76264762010-04-02 22:27:05 +00001801 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001802 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001803
Sean Callanan18b83232010-01-19 21:44:56 +00001804 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001805 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001806 // None of addrmode3 instruction uses "!"
1807 if (AddrMode == ARMII::AddrMode3)
1808 return true;
1809
Bill Wendling50d0f582010-11-18 23:43:05 +00001810 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1811 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001812 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001813 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001814 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1815 if (AddrMode == ARMII::AddrMode2)
1816 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001817 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001818 } else {
1819 // The "[Rn" we have so far was not followed by a comma.
1820
Jim Grosbach80eb2332010-10-29 17:41:25 +00001821 // If there's anything other than the right brace, this is a post indexing
1822 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001823 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001824 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001825
Sean Callanan18b83232010-01-19 21:44:56 +00001826 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001827
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001828 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001829 Postindexed = true;
1830 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001831
Chris Lattner550276e2010-10-28 20:52:15 +00001832 if (NextTok.isNot(AsmToken::Comma)) {
1833 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001834 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001835 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001836
Sean Callananb9a25b72010-01-19 20:27:46 +00001837 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001838
Chris Lattner550276e2010-10-28 20:52:15 +00001839 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001840 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001841 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001842 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001843 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001844 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001845
1846 // Force Offset to exist if used.
1847 if (!OffsetIsReg) {
1848 if (!Offset)
1849 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001850 } else {
1851 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1852 Error(E, "shift amount not supported");
1853 return true;
1854 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001855 }
1856
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001857 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1858 Offset, OffsetRegNum, OffsetRegShifted,
1859 ShiftType, ShiftAmount, Preindexed,
1860 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001861 if (WBOp)
1862 Operands.push_back(WBOp);
1863
1864 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001865}
1866
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001867/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1868/// we will parse the following (were +/- means that a plus or minus is
1869/// optional):
1870/// +/-Rm
1871/// +/-Rm, shift
1872/// #offset
1873/// we return false on success or an error otherwise.
1874bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001875 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001876 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001877 const MCExpr *&ShiftAmount,
1878 const MCExpr *&Offset,
1879 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001880 int &OffsetRegNum,
1881 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001882 Negative = false;
1883 OffsetRegShifted = false;
1884 OffsetIsReg = false;
1885 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001886 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001887 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001888 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001889 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001890 else if (NextTok.is(AsmToken::Minus)) {
1891 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001892 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001893 }
1894 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001895 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001896 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001897 SMLoc CurLoc = OffsetRegTok.getLoc();
1898 OffsetRegNum = TryParseRegister();
1899 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001900 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001901 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001902 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001903 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001904
Bill Wendling12f40e92010-11-06 10:51:53 +00001905 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001906 if (OffsetRegNum != -1) {
1907 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001908 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001909 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001910 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001911
Sean Callanan18b83232010-01-19 21:44:56 +00001912 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001913 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001914 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001915 OffsetRegShifted = true;
1916 }
1917 }
1918 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1919 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001920 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001921 if (HashTok.isNot(AsmToken::Hash))
1922 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001923
Sean Callananb9a25b72010-01-19 20:27:46 +00001924 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001925
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001926 if (getParser().ParseExpression(Offset))
1927 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001928 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001929 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001930 return false;
1931}
1932
1933/// ParseShift as one of these two:
1934/// ( lsl | lsr | asr | ror ) , # shift_amount
1935/// rrx
1936/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001937bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1938 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001939 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001940 if (Tok.isNot(AsmToken::Identifier))
1941 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001942 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001943 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001944 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001945 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001946 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001947 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001948 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001949 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001950 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001951 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001952 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001953 else
1954 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001955 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001956
1957 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001958 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001959 return false;
1960
1961 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001962 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001963 if (HashTok.isNot(AsmToken::Hash))
1964 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001965 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001966
1967 if (getParser().ParseExpression(ShiftAmount))
1968 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001969
1970 return false;
1971}
1972
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001973/// Parse a arm instruction operand. For now this parses the operand regardless
1974/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001975bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001976 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001977 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001978
1979 // Check if the current operand has a custom associated parser, if so, try to
1980 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001981 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1982 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001983 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001984 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1985 // there was a match, but an error occurred, in which case, just return that
1986 // the operand parsing failed.
1987 if (ResTy == MatchOperand_ParseFail)
1988 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001989
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001990 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001991 default:
1992 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001993 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001994 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001995 if (!TryParseRegisterWithWriteBack(Operands))
1996 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001997 int Res = TryParseShiftRegister(Operands);
1998 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001999 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00002000 else if (Res == -1) // irrecoverable error
2001 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002002
2003 // Fall though for the Identifier case that is not a register or a
2004 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00002005 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00002006 case AsmToken::Integer: // things like 1f and 2b as a branch targets
2007 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00002008 // This was not a register so parse other operands that start with an
2009 // identifier (like labels) as expressions and create them as immediates.
2010 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00002011 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00002012 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00002013 return true;
Sean Callanan76264762010-04-02 22:27:05 +00002014 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00002015 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
2016 return false;
2017 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002018 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00002019 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002020 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00002021 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002022 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00002023 // #42 -> immediate.
2024 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00002025 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00002026 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002027 const MCExpr *ImmVal;
2028 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00002029 return true;
Sean Callanan76264762010-04-02 22:27:05 +00002030 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00002031 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
2032 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002033 case AsmToken::Colon: {
2034 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00002035 // FIXME: Check it's an expression prefix,
2036 // e.g. (FOO - :lower16:BAR) isn't legal.
2037 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002038 if (ParsePrefix(RefKind))
2039 return true;
2040
Evan Cheng75972122011-01-13 07:58:56 +00002041 const MCExpr *SubExprVal;
2042 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00002043 return true;
2044
Evan Cheng75972122011-01-13 07:58:56 +00002045 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
2046 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00002047 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00002048 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00002049 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002050 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00002051 }
2052}
2053
Evan Cheng75972122011-01-13 07:58:56 +00002054// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
2055// :lower16: and :upper16:.
2056bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
2057 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002058
2059 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00002060 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00002061 Parser.Lex(); // Eat ':'
2062
2063 if (getLexer().isNot(AsmToken::Identifier)) {
2064 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
2065 return true;
2066 }
2067
2068 StringRef IDVal = Parser.getTok().getIdentifier();
2069 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00002070 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002071 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00002072 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002073 } else {
2074 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
2075 return true;
2076 }
2077 Parser.Lex();
2078
2079 if (getLexer().isNot(AsmToken::Colon)) {
2080 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
2081 return true;
2082 }
2083 Parser.Lex(); // Eat the last ':'
2084 return false;
2085}
2086
2087const MCExpr *
2088ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
2089 MCSymbolRefExpr::VariantKind Variant) {
2090 // Recurse over the given expression, rebuilding it to apply the given variant
2091 // to the leftmost symbol.
2092 if (Variant == MCSymbolRefExpr::VK_None)
2093 return E;
2094
2095 switch (E->getKind()) {
2096 case MCExpr::Target:
2097 llvm_unreachable("Can't handle target expr yet");
2098 case MCExpr::Constant:
2099 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
2100
2101 case MCExpr::SymbolRef: {
2102 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
2103
2104 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
2105 return 0;
2106
2107 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
2108 }
2109
2110 case MCExpr::Unary:
2111 llvm_unreachable("Can't handle unary expressions yet");
2112
2113 case MCExpr::Binary: {
2114 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
2115 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
2116 const MCExpr *RHS = BE->getRHS();
2117 if (!LHS)
2118 return 0;
2119
2120 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
2121 }
2122 }
2123
2124 assert(0 && "Invalid expression kind!");
2125 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002126}
2127
Daniel Dunbar352e1482011-01-11 15:59:50 +00002128/// \brief Given a mnemonic, split out possible predication code and carry
2129/// setting letters to form a canonical mnemonic and flags.
2130//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002131// FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00002132StringRef ARMAsmParser::SplitMnemonic(StringRef Mnemonic,
2133 unsigned &PredicationCode,
2134 bool &CarrySetting,
2135 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00002136 PredicationCode = ARMCC::AL;
2137 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002138 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002139
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002140 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00002141 //
2142 // FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00002143 if ((Mnemonic == "movs" && isThumb()) ||
2144 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
2145 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
2146 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
2147 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
2148 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
2149 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
2150 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
Daniel Dunbar352e1482011-01-11 15:59:50 +00002151 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00002152
Jim Grosbach3f00e312011-07-11 17:09:57 +00002153 // First, split out any predication code. Ignore mnemonics we know aren't
2154 // predicated but do have a carry-set and so weren't caught above.
Jim Grosbachab40f4b2011-07-20 18:20:31 +00002155 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
2156 Mnemonic != "muls") {
Jim Grosbach3f00e312011-07-11 17:09:57 +00002157 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
2158 .Case("eq", ARMCC::EQ)
2159 .Case("ne", ARMCC::NE)
2160 .Case("hs", ARMCC::HS)
2161 .Case("cs", ARMCC::HS)
2162 .Case("lo", ARMCC::LO)
2163 .Case("cc", ARMCC::LO)
2164 .Case("mi", ARMCC::MI)
2165 .Case("pl", ARMCC::PL)
2166 .Case("vs", ARMCC::VS)
2167 .Case("vc", ARMCC::VC)
2168 .Case("hi", ARMCC::HI)
2169 .Case("ls", ARMCC::LS)
2170 .Case("ge", ARMCC::GE)
2171 .Case("lt", ARMCC::LT)
2172 .Case("gt", ARMCC::GT)
2173 .Case("le", ARMCC::LE)
2174 .Case("al", ARMCC::AL)
2175 .Default(~0U);
2176 if (CC != ~0U) {
2177 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
2178 PredicationCode = CC;
2179 }
Bill Wendling52925b62010-10-29 23:50:21 +00002180 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002181
Daniel Dunbar352e1482011-01-11 15:59:50 +00002182 // Next, determine if we have a carry setting bit. We explicitly ignore all
2183 // the instructions we know end in 's'.
2184 if (Mnemonic.endswith("s") &&
2185 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002186 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
2187 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
2188 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
2189 Mnemonic == "vrsqrts" || (Mnemonic == "movs" && isThumb()))) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00002190 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
2191 CarrySetting = true;
2192 }
2193
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002194 // The "cps" instruction can have a interrupt mode operand which is glued into
2195 // the mnemonic. Check if this is the case, split it and parse the imod op
2196 if (Mnemonic.startswith("cps")) {
2197 // Split out any imod code.
2198 unsigned IMod =
2199 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
2200 .Case("ie", ARM_PROC::IE)
2201 .Case("id", ARM_PROC::ID)
2202 .Default(~0U);
2203 if (IMod != ~0U) {
2204 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2205 ProcessorIMod = IMod;
2206 }
2207 }
2208
Daniel Dunbar352e1482011-01-11 15:59:50 +00002209 return Mnemonic;
2210}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002211
2212/// \brief Given a canonical mnemonic, determine if the instruction ever allows
2213/// inclusion of carry set or predication code operands.
2214//
2215// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00002216void ARMAsmParser::
2217GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2218 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002219 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2220 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2221 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2222 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002223 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002224 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2225 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002226 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00002227 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002228 CanAcceptCarrySet = true;
2229 } else {
2230 CanAcceptCarrySet = false;
2231 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002232
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002233 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2234 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2235 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2236 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002237 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "clrex" ||
Jim Grosbachc27d4f92011-07-22 17:44:50 +00002238 Mnemonic == "setend" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002239 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumb())) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002240 CanAcceptPredicationCode = false;
2241 } else {
2242 CanAcceptPredicationCode = true;
2243 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002244
Evan Chengebdeeab2011-07-08 01:53:10 +00002245 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002246 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002247 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002248 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002249}
2250
2251/// Parse an arm instruction mnemonic followed by its operands.
2252bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2253 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2254 // Create the leading tokens for the mnemonic, split by '.' characters.
2255 size_t Start = 0, Next = Name.find('.');
Jim Grosbachffa32252011-07-19 19:13:28 +00002256 StringRef Mnemonic = Name.slice(Start, Next);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002257
Daniel Dunbar352e1482011-01-11 15:59:50 +00002258 // Split out the predication code and carry setting flag from the mnemonic.
2259 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002260 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002261 bool CarrySetting;
Jim Grosbachffa32252011-07-19 19:13:28 +00002262 Mnemonic = SplitMnemonic(Mnemonic, PredicationCode, CarrySetting,
Jim Grosbachc27d4f92011-07-22 17:44:50 +00002263 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002264
Jim Grosbachffa32252011-07-19 19:13:28 +00002265 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
2266
2267 // FIXME: This is all a pretty gross hack. We should automatically handle
2268 // optional operands like this via tblgen.
Bill Wendling9717fa92010-11-21 10:56:05 +00002269
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002270 // Next, add the CCOut and ConditionCode operands, if needed.
2271 //
2272 // For mnemonics which can ever incorporate a carry setting bit or predication
2273 // code, our matching model involves us always generating CCOut and
2274 // ConditionCode operands to match the mnemonic "as written" and then we let
2275 // the matcher deal with finding the right instruction or generating an
2276 // appropriate error.
2277 bool CanAcceptCarrySet, CanAcceptPredicationCode;
Jim Grosbachffa32252011-07-19 19:13:28 +00002278 GetMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002279
Jim Grosbach33c16a22011-07-14 22:04:21 +00002280 // If we had a carry-set on an instruction that can't do that, issue an
2281 // error.
2282 if (!CanAcceptCarrySet && CarrySetting) {
2283 Parser.EatToEndOfStatement();
Jim Grosbachffa32252011-07-19 19:13:28 +00002284 return Error(NameLoc, "instruction '" + Mnemonic +
Jim Grosbach33c16a22011-07-14 22:04:21 +00002285 "' can not set flags, but 's' suffix specified");
2286 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +00002287 // If we had a predication code on an instruction that can't do that, issue an
2288 // error.
2289 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
2290 Parser.EatToEndOfStatement();
2291 return Error(NameLoc, "instruction '" + Mnemonic +
2292 "' is not predicable, but condition code specified");
2293 }
Jim Grosbach33c16a22011-07-14 22:04:21 +00002294
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002295 // Add the carry setting operand, if necessary.
2296 //
2297 // FIXME: It would be awesome if we could somehow invent a location such that
2298 // match errors on this operand would print a nice diagnostic about how the
2299 // 's' character in the mnemonic resulted in a CCOut operand.
Jim Grosbach33c16a22011-07-14 22:04:21 +00002300 if (CanAcceptCarrySet)
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002301 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2302 NameLoc));
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002303
2304 // Add the predication code operand, if necessary.
2305 if (CanAcceptPredicationCode) {
2306 Operands.push_back(ARMOperand::CreateCondCode(
2307 ARMCC::CondCodes(PredicationCode), NameLoc));
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002308 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002309
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002310 // Add the processor imod operand, if necessary.
2311 if (ProcessorIMod) {
2312 Operands.push_back(ARMOperand::CreateImm(
2313 MCConstantExpr::Create(ProcessorIMod, getContext()),
2314 NameLoc, NameLoc));
2315 } else {
2316 // This mnemonic can't ever accept a imod, but the user wrote
2317 // one (or misspelled another mnemonic).
2318
2319 // FIXME: Issue a nice error.
2320 }
2321
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002322 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002323 while (Next != StringRef::npos) {
2324 Start = Next;
2325 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002326 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002327
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002328 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002329 }
2330
2331 // Read the remaining operands.
2332 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002333 // Read the first operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002334 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002335 Parser.EatToEndOfStatement();
2336 return true;
2337 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002338
2339 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002340 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002341
2342 // Parse and remember the operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002343 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002344 Parser.EatToEndOfStatement();
2345 return true;
2346 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002347 }
2348 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002349
Chris Lattnercbf8a982010-09-11 16:18:25 +00002350 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2351 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002352 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002353 }
Bill Wendling146018f2010-11-06 21:42:12 +00002354
Chris Lattner34e53142010-09-08 05:10:46 +00002355 Parser.Lex(); // Consume the EndOfStatement
Jim Grosbachffa32252011-07-19 19:13:28 +00002356
2357
2358 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
2359 // another does not. Specifically, the MOVW instruction does not. So we
2360 // special case it here and remove the defaulted (non-setting) cc_out
2361 // operand if that's the instruction we're trying to match.
2362 //
2363 // We do this post-processing of the explicit operands rather than just
2364 // conditionally adding the cc_out in the first place because we need
2365 // to check the type of the parsed immediate operand.
2366 if (Mnemonic == "mov" && Operands.size() > 4 &&
2367 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
Jim Grosbach731f2092011-07-19 19:45:44 +00002368 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
2369 static_cast<ARMOperand*>(Operands[1])->getReg() == 0) {
Jim Grosbachffa32252011-07-19 19:13:28 +00002370 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
2371 Operands.erase(Operands.begin() + 1);
2372 delete Op;
2373 }
2374
Chris Lattner98986712010-01-14 22:21:20 +00002375 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002376}
2377
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002378bool ARMAsmParser::
2379MatchAndEmitInstruction(SMLoc IDLoc,
2380 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2381 MCStreamer &Out) {
2382 MCInst Inst;
2383 unsigned ErrorInfo;
Jim Grosbach5a187002011-07-19 18:32:48 +00002384 MatchResultTy MatchResult;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002385 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002386 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002387 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002388 Out.EmitInstruction(Inst);
2389 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002390 case Match_MissingFeature:
2391 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2392 return true;
2393 case Match_InvalidOperand: {
2394 SMLoc ErrorLoc = IDLoc;
2395 if (ErrorInfo != ~0U) {
2396 if (ErrorInfo >= Operands.size())
2397 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002398
Chris Lattnere73d4f82010-10-28 21:41:58 +00002399 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2400 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2401 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002402
Chris Lattnere73d4f82010-10-28 21:41:58 +00002403 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002404 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002405 case Match_MnemonicFail:
2406 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002407 case Match_ConversionFail:
2408 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002409 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002410
Eric Christopherc223e2b2010-10-29 09:26:59 +00002411 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002412 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002413}
2414
Kevin Enderby515d5092009-10-15 20:48:48 +00002415/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002416bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2417 StringRef IDVal = DirectiveID.getIdentifier();
2418 if (IDVal == ".word")
2419 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002420 else if (IDVal == ".thumb")
2421 return ParseDirectiveThumb(DirectiveID.getLoc());
2422 else if (IDVal == ".thumb_func")
2423 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2424 else if (IDVal == ".code")
2425 return ParseDirectiveCode(DirectiveID.getLoc());
2426 else if (IDVal == ".syntax")
2427 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002428 return true;
2429}
2430
2431/// ParseDirectiveWord
2432/// ::= .word [ expression (, expression)* ]
2433bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2434 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2435 for (;;) {
2436 const MCExpr *Value;
2437 if (getParser().ParseExpression(Value))
2438 return true;
2439
Chris Lattneraaec2052010-01-19 19:46:13 +00002440 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002441
2442 if (getLexer().is(AsmToken::EndOfStatement))
2443 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002444
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002445 // FIXME: Improve diagnostic.
2446 if (getLexer().isNot(AsmToken::Comma))
2447 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002448 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002449 }
2450 }
2451
Sean Callananb9a25b72010-01-19 20:27:46 +00002452 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002453 return false;
2454}
2455
Kevin Enderby515d5092009-10-15 20:48:48 +00002456/// ParseDirectiveThumb
2457/// ::= .thumb
2458bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2459 if (getLexer().isNot(AsmToken::EndOfStatement))
2460 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002461 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002462
2463 // TODO: set thumb mode
2464 // TODO: tell the MC streamer the mode
2465 // getParser().getStreamer().Emit???();
2466 return false;
2467}
2468
2469/// ParseDirectiveThumbFunc
2470/// ::= .thumbfunc symbol_name
2471bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002472 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2473 bool isMachO = MAI.hasSubsectionsViaSymbols();
2474 StringRef Name;
2475
2476 // Darwin asm has function name after .thumb_func direction
2477 // ELF doesn't
2478 if (isMachO) {
2479 const AsmToken &Tok = Parser.getTok();
2480 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2481 return Error(L, "unexpected token in .thumb_func directive");
2482 Name = Tok.getString();
2483 Parser.Lex(); // Consume the identifier token.
2484 }
2485
Kevin Enderby515d5092009-10-15 20:48:48 +00002486 if (getLexer().isNot(AsmToken::EndOfStatement))
2487 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002488 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002489
Rafael Espindola64695402011-05-16 16:17:21 +00002490 // FIXME: assuming function name will be the line following .thumb_func
2491 if (!isMachO) {
2492 Name = Parser.getTok().getString();
2493 }
2494
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002495 // Mark symbol as a thumb symbol.
2496 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2497 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002498 return false;
2499}
2500
2501/// ParseDirectiveSyntax
2502/// ::= .syntax unified | divided
2503bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002504 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002505 if (Tok.isNot(AsmToken::Identifier))
2506 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002507 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002508 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002509 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002510 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002511 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002512 else
2513 return Error(L, "unrecognized syntax mode in .syntax directive");
2514
2515 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002516 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002517 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002518
2519 // TODO tell the MC streamer the mode
2520 // getParser().getStreamer().Emit???();
2521 return false;
2522}
2523
2524/// ParseDirectiveCode
2525/// ::= .code 16 | 32
2526bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002527 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002528 if (Tok.isNot(AsmToken::Integer))
2529 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002530 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002531 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002532 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002533 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002534 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002535 else
2536 return Error(L, "invalid operand to .code directive");
2537
2538 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002539 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002540 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002541
Evan Cheng32869202011-07-08 22:36:29 +00002542 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002543 if (!isThumb())
2544 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002545 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002546 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002547 if (isThumb())
2548 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002549 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002550 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002551
Kevin Enderby515d5092009-10-15 20:48:48 +00002552 return false;
2553}
2554
Sean Callanan90b70972010-04-07 20:29:34 +00002555extern "C" void LLVMInitializeARMAsmLexer();
2556
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002557/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002558extern "C" void LLVMInitializeARMAsmParser() {
2559 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2560 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002561 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002562}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002563
Chris Lattner0692ee62010-09-06 19:11:01 +00002564#define GET_REGISTER_MATCHER
2565#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002566#include "ARMGenAsmMatcher.inc"