blob: da07ea08507a42c1a7be8c6253f476c6e4c67808 [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 Grosbach4a5ffb32011-07-22 23:16:18 +0000430 bool isImm1_32() 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 < 33;
437 }
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000438 bool isImm0_65535() const {
439 if (Kind != Immediate)
440 return false;
441 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
442 if (!CE) return false;
443 int64_t Value = CE->getValue();
444 return Value >= 0 && Value < 65536;
445 }
Jim Grosbachffa32252011-07-19 19:13:28 +0000446 bool isImm0_65535Expr() const {
447 if (Kind != Immediate)
448 return false;
449 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
450 // If it's not a constant expression, it'll generate a fixup and be
451 // handled later.
452 if (!CE) return true;
453 int64_t Value = CE->getValue();
454 return Value >= 0 && Value < 65536;
455 }
Jim Grosbachf6c05252011-07-21 17:23:04 +0000456 bool isPKHLSLImm() 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 }
464 bool isPKHASRImm() 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 Value > 0 && Value <= 32;
471 }
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000472 bool isARMSOImm() 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::getSOImmVal(Value) != -1;
479 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000480 bool isT2SOImm() 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 ARM_AM::getT2SOImmVal(Value) != -1;
487 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000488 bool isSetEndImm() const {
489 if (Kind != Immediate)
490 return false;
491 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
492 if (!CE) return false;
493 int64_t Value = CE->getValue();
494 return Value == 1 || Value == 0;
495 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000496 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000497 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000498 bool isDPRRegList() const { return Kind == DPRRegisterList; }
499 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000500 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000501 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000502 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000503 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000504 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Owen Anderson92a20222011-07-21 18:54:16 +0000505 bool isShiftedImm() const { return Kind == ShiftedImmediate; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000506 bool isMemMode2() const {
507 if (getMemAddrMode() != ARMII::AddrMode2)
508 return false;
509
510 if (getMemOffsetIsReg())
511 return true;
512
513 if (getMemNegative() &&
514 !(getMemPostindexed() || getMemPreindexed()))
515 return false;
516
517 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
518 if (!CE) return false;
519 int64_t Value = CE->getValue();
520
521 // The offset must be in the range 0-4095 (imm12).
522 if (Value > 4095 || Value < -4095)
523 return false;
524
525 return true;
526 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000527 bool isMemMode3() const {
528 if (getMemAddrMode() != ARMII::AddrMode3)
529 return false;
530
531 if (getMemOffsetIsReg()) {
532 if (getMemOffsetRegShifted())
533 return false; // No shift with offset reg allowed
534 return true;
535 }
536
537 if (getMemNegative() &&
538 !(getMemPostindexed() || getMemPreindexed()))
539 return false;
540
541 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
542 if (!CE) return false;
543 int64_t Value = CE->getValue();
544
545 // The offset must be in the range 0-255 (imm8).
546 if (Value > 255 || Value < -255)
547 return false;
548
549 return true;
550 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000551 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000552 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
553 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000554 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000555
Daniel Dunbar4b462672011-01-18 05:55:27 +0000556 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000557 if (!CE) return false;
558
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000559 // The offset must be a multiple of 4 in the range 0-1020.
560 int64_t Value = CE->getValue();
561 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
562 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000563 bool isMemMode7() const {
564 if (!isMemory() ||
565 getMemPreindexed() ||
566 getMemPostindexed() ||
567 getMemOffsetIsReg() ||
568 getMemNegative() ||
569 getMemWriteback())
570 return false;
571
572 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
573 if (!CE) return false;
574
575 if (CE->getValue())
576 return false;
577
578 return true;
579 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000580 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000581 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000582 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000583 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000584 }
585 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000586 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000587 return false;
588
Daniel Dunbar4b462672011-01-18 05:55:27 +0000589 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000590 if (!CE) return false;
591
592 // The offset must be a multiple of 4 in the range 0-124.
593 uint64_t Value = CE->getValue();
594 return ((Value & 0x3) == 0 && Value <= 124);
595 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000596 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000597 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000598
599 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000600 // Add as immediates when possible. Null MCExpr = 0.
601 if (Expr == 0)
602 Inst.addOperand(MCOperand::CreateImm(0));
603 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000604 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
605 else
606 Inst.addOperand(MCOperand::CreateExpr(Expr));
607 }
608
Daniel Dunbar8462b302010-08-11 06:36:53 +0000609 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000610 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000611 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000612 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
613 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000614 }
615
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000616 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
617 assert(N == 1 && "Invalid number of operands!");
618 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
619 }
620
621 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
622 assert(N == 1 && "Invalid number of operands!");
623 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
624 }
625
Jim Grosbachd67641b2010-12-06 18:21:12 +0000626 void addCCOutOperands(MCInst &Inst, unsigned N) const {
627 assert(N == 1 && "Invalid number of operands!");
628 Inst.addOperand(MCOperand::CreateReg(getReg()));
629 }
630
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000631 void addRegOperands(MCInst &Inst, unsigned N) const {
632 assert(N == 1 && "Invalid number of operands!");
633 Inst.addOperand(MCOperand::CreateReg(getReg()));
634 }
635
Jim Grosbache8606dc2011-07-13 17:50:29 +0000636 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
637 assert(N == 3 && "Invalid number of operands!");
638 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
Jim Grosbache8606dc2011-07-13 17:50:29 +0000639 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
640 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
641 Inst.addOperand(MCOperand::CreateImm(
642 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
643 }
644
Owen Anderson92a20222011-07-21 18:54:16 +0000645 void addShiftedImmOperands(MCInst &Inst, unsigned N) const {
Owen Anderson152d4a42011-07-21 23:38:37 +0000646 assert(N == 2 && "Invalid number of operands!");
Owen Anderson92a20222011-07-21 18:54:16 +0000647 assert(isShiftedImm() && "addShiftedImmOperands() on non ShiftedImm!");
648 Inst.addOperand(MCOperand::CreateReg(ShiftedImm.SrcReg));
Owen Anderson92a20222011-07-21 18:54:16 +0000649 Inst.addOperand(MCOperand::CreateImm(
650 ARM_AM::getSORegOpc(ShiftedImm.ShiftTy, ShiftedImm.ShiftImm)));
651 }
652
653
Owen Anderson00828302011-03-18 22:50:18 +0000654 void addShifterOperands(MCInst &Inst, unsigned N) const {
655 assert(N == 1 && "Invalid number of operands!");
656 Inst.addOperand(MCOperand::CreateImm(
657 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
658 }
659
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000660 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000661 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000662 const SmallVectorImpl<unsigned> &RegList = getRegList();
663 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000664 I = RegList.begin(), E = RegList.end(); I != E; ++I)
665 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000666 }
667
Bill Wendling0f630752010-11-17 04:32:08 +0000668 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
669 addRegListOperands(Inst, N);
670 }
671
672 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
673 addRegListOperands(Inst, N);
674 }
675
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000676 void addImmOperands(MCInst &Inst, unsigned N) const {
677 assert(N == 1 && "Invalid number of operands!");
678 addExpr(Inst, getImm());
679 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000680
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000681 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
682 assert(N == 1 && "Invalid number of operands!");
683 addExpr(Inst, getImm());
684 }
685
Jim Grosbach83ab0702011-07-13 22:01:08 +0000686 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
687 assert(N == 1 && "Invalid number of operands!");
688 addExpr(Inst, getImm());
689 }
690
691 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
692 assert(N == 1 && "Invalid number of operands!");
693 addExpr(Inst, getImm());
694 }
695
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000696 void addImm0_31Operands(MCInst &Inst, unsigned N) const {
697 assert(N == 1 && "Invalid number of operands!");
698 addExpr(Inst, getImm());
699 }
700
Jim Grosbach4a5ffb32011-07-22 23:16:18 +0000701 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
702 assert(N == 1 && "Invalid number of operands!");
703 // The constant encodes as the immediate-1, and we store in the instruction
704 // the bits as encoded, so subtract off one here.
705 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
706 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
707 }
708
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000709 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
710 assert(N == 1 && "Invalid number of operands!");
711 addExpr(Inst, getImm());
712 }
713
Jim Grosbachffa32252011-07-19 19:13:28 +0000714 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
715 assert(N == 1 && "Invalid number of operands!");
716 addExpr(Inst, getImm());
717 }
718
Jim Grosbachf6c05252011-07-21 17:23:04 +0000719 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
720 assert(N == 1 && "Invalid number of operands!");
721 addExpr(Inst, getImm());
722 }
723
724 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
725 assert(N == 1 && "Invalid number of operands!");
726 // An ASR value of 32 encodes as 0, so that's how we want to add it to
727 // the instruction as well.
728 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
729 int Val = CE->getValue();
730 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
731 }
732
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000733 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
734 assert(N == 1 && "Invalid number of operands!");
735 addExpr(Inst, getImm());
736 }
737
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000738 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
739 assert(N == 1 && "Invalid number of operands!");
740 addExpr(Inst, getImm());
741 }
742
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000743 void addSetEndImmOperands(MCInst &Inst, unsigned N) const {
744 assert(N == 1 && "Invalid number of operands!");
745 addExpr(Inst, getImm());
746 }
747
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000748 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
749 assert(N == 1 && "Invalid number of operands!");
750 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
751 }
752
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000753 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
754 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
755 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
756
757 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000758 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000759 assert((CE || CE->getValue() == 0) &&
760 "No offset operand support in mode 7");
761 }
762
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000763 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
764 assert(isMemMode2() && "Invalid mode or number of operands!");
765 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
766 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
767
768 if (getMemOffsetIsReg()) {
769 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
770
771 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
772 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
773 int64_t ShiftAmount = 0;
774
775 if (getMemOffsetRegShifted()) {
776 ShOpc = getMemShiftType();
777 const MCConstantExpr *CE =
778 dyn_cast<MCConstantExpr>(getMemShiftAmount());
779 ShiftAmount = CE->getValue();
780 }
781
782 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
783 ShOpc, IdxMode)));
784 return;
785 }
786
787 // Create a operand placeholder to always yield the same number of operands.
788 Inst.addOperand(MCOperand::CreateReg(0));
789
790 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
791 // the difference?
792 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
793 assert(CE && "Non-constant mode 2 offset operand!");
794 int64_t Offset = CE->getValue();
795
796 if (Offset >= 0)
797 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
798 Offset, ARM_AM::no_shift, IdxMode)));
799 else
800 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
801 -Offset, ARM_AM::no_shift, IdxMode)));
802 }
803
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000804 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
805 assert(isMemMode3() && "Invalid mode or number of operands!");
806 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
807 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
808
809 if (getMemOffsetIsReg()) {
810 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
811
812 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
813 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
814 IdxMode)));
815 return;
816 }
817
818 // Create a operand placeholder to always yield the same number of operands.
819 Inst.addOperand(MCOperand::CreateReg(0));
820
821 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
822 // the difference?
823 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
824 assert(CE && "Non-constant mode 3 offset operand!");
825 int64_t Offset = CE->getValue();
826
827 if (Offset >= 0)
828 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
829 Offset, IdxMode)));
830 else
831 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
832 -Offset, IdxMode)));
833 }
834
Chris Lattner14b93852010-10-29 00:27:31 +0000835 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
836 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000837
Daniel Dunbar4b462672011-01-18 05:55:27 +0000838 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
839 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000840
Jim Grosbach80eb2332010-10-29 17:41:25 +0000841 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
842 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000843 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000844 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000845
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000846 // The MCInst offset operand doesn't include the low two bits (like
847 // the instruction encoding).
848 int64_t Offset = CE->getValue() / 4;
849 if (Offset >= 0)
850 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
851 Offset)));
852 else
853 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
854 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000855 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000856
Bill Wendlingf4caf692010-12-14 03:36:38 +0000857 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
858 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000859 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
860 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000861 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000862
Bill Wendlingf4caf692010-12-14 03:36:38 +0000863 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
864 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000865 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
866 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000867 assert(CE && "Non-constant mode offset operand!");
868 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000869 }
870
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000871 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
872 assert(N == 1 && "Invalid number of operands!");
873 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
874 }
875
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000876 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
877 assert(N == 1 && "Invalid number of operands!");
878 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
879 }
880
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000881 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000882
Chris Lattner3a697562010-10-28 17:20:03 +0000883 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
884 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000885 Op->CC.Val = CC;
886 Op->StartLoc = S;
887 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000888 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000889 }
890
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000891 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
892 ARMOperand *Op = new ARMOperand(CoprocNum);
893 Op->Cop.Val = CopVal;
894 Op->StartLoc = S;
895 Op->EndLoc = S;
896 return Op;
897 }
898
899 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
900 ARMOperand *Op = new ARMOperand(CoprocReg);
901 Op->Cop.Val = CopVal;
902 Op->StartLoc = S;
903 Op->EndLoc = S;
904 return Op;
905 }
906
Jim Grosbachd67641b2010-12-06 18:21:12 +0000907 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
908 ARMOperand *Op = new ARMOperand(CCOut);
909 Op->Reg.RegNum = RegNum;
910 Op->StartLoc = S;
911 Op->EndLoc = S;
912 return Op;
913 }
914
Chris Lattner3a697562010-10-28 17:20:03 +0000915 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
916 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000917 Op->Tok.Data = Str.data();
918 Op->Tok.Length = Str.size();
919 Op->StartLoc = S;
920 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000921 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000922 }
923
Bill Wendling50d0f582010-11-18 23:43:05 +0000924 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000925 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000926 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000927 Op->StartLoc = S;
928 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000929 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000930 }
931
Jim Grosbache8606dc2011-07-13 17:50:29 +0000932 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
933 unsigned SrcReg,
934 unsigned ShiftReg,
935 unsigned ShiftImm,
936 SMLoc S, SMLoc E) {
937 ARMOperand *Op = new ARMOperand(ShiftedRegister);
938 Op->ShiftedReg.ShiftTy = ShTy;
939 Op->ShiftedReg.SrcReg = SrcReg;
940 Op->ShiftedReg.ShiftReg = ShiftReg;
941 Op->ShiftedReg.ShiftImm = ShiftImm;
942 Op->StartLoc = S;
943 Op->EndLoc = E;
944 return Op;
945 }
946
Owen Anderson92a20222011-07-21 18:54:16 +0000947 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
948 unsigned SrcReg,
949 unsigned ShiftImm,
950 SMLoc S, SMLoc E) {
951 ARMOperand *Op = new ARMOperand(ShiftedImmediate);
952 Op->ShiftedImm.ShiftTy = ShTy;
953 Op->ShiftedImm.SrcReg = SrcReg;
954 Op->ShiftedImm.ShiftImm = ShiftImm;
955 Op->StartLoc = S;
956 Op->EndLoc = E;
957 return Op;
958 }
959
Owen Anderson00828302011-03-18 22:50:18 +0000960 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
961 SMLoc S, SMLoc E) {
962 ARMOperand *Op = new ARMOperand(Shifter);
963 Op->Shift.ShiftTy = ShTy;
964 Op->StartLoc = S;
965 Op->EndLoc = E;
966 return Op;
967 }
968
Bill Wendling7729e062010-11-09 22:44:22 +0000969 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000970 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000971 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000972 KindTy Kind = RegisterList;
973
974 if (ARM::DPRRegClass.contains(Regs.front().first))
975 Kind = DPRRegisterList;
976 else if (ARM::SPRRegClass.contains(Regs.front().first))
977 Kind = SPRRegisterList;
978
979 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000980 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000981 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000982 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000983 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000984 Op->StartLoc = StartLoc;
985 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000986 return Op;
987 }
988
Chris Lattner3a697562010-10-28 17:20:03 +0000989 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
990 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000991 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000992 Op->StartLoc = S;
993 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000994 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000995 }
996
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000997 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
998 bool OffsetIsReg, const MCExpr *Offset,
999 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001000 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +00001001 const MCExpr *ShiftAmount, bool Preindexed,
1002 bool Postindexed, bool Negative, bool Writeback,
1003 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +00001004 assert((OffsetRegNum == -1 || OffsetIsReg) &&
1005 "OffsetRegNum must imply OffsetIsReg!");
1006 assert((!OffsetRegShifted || OffsetIsReg) &&
1007 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +00001008 assert((Offset || OffsetIsReg) &&
1009 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +00001010 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
1011 "Cannot have shift amount without shifted register offset!");
1012 assert((!Offset || !OffsetIsReg) &&
1013 "Cannot have expression offset and register offset!");
1014
Chris Lattner3a697562010-10-28 17:20:03 +00001015 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001016 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +00001017 Op->Mem.BaseRegNum = BaseRegNum;
1018 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +00001019 if (OffsetIsReg)
1020 Op->Mem.Offset.RegNum = OffsetRegNum;
1021 else
1022 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +00001023 Op->Mem.OffsetRegShifted = OffsetRegShifted;
1024 Op->Mem.ShiftType = ShiftType;
1025 Op->Mem.ShiftAmount = ShiftAmount;
1026 Op->Mem.Preindexed = Preindexed;
1027 Op->Mem.Postindexed = Postindexed;
1028 Op->Mem.Negative = Negative;
1029 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +00001030
Sean Callanan76264762010-04-02 22:27:05 +00001031 Op->StartLoc = S;
1032 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001033 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001034 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001035
1036 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
1037 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
1038 Op->MBOpt.Val = Opt;
1039 Op->StartLoc = S;
1040 Op->EndLoc = S;
1041 return Op;
1042 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001043
1044 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1045 ARMOperand *Op = new ARMOperand(ProcIFlags);
1046 Op->IFlags.Val = IFlags;
1047 Op->StartLoc = S;
1048 Op->EndLoc = S;
1049 return Op;
1050 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001051
1052 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1053 ARMOperand *Op = new ARMOperand(MSRMask);
1054 Op->MMask.Val = MMask;
1055 Op->StartLoc = S;
1056 Op->EndLoc = S;
1057 return Op;
1058 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001059};
1060
1061} // end anonymous namespace.
1062
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001063void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001064 switch (Kind) {
1065 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +00001066 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001067 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +00001068 case CCOut:
1069 OS << "<ccout " << getReg() << ">";
1070 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001071 case CoprocNum:
1072 OS << "<coprocessor number: " << getCoproc() << ">";
1073 break;
1074 case CoprocReg:
1075 OS << "<coprocessor register: " << getCoproc() << ">";
1076 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001077 case MSRMask:
1078 OS << "<mask: " << getMSRMask() << ">";
1079 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001080 case Immediate:
1081 getImm()->print(OS);
1082 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001083 case MemBarrierOpt:
1084 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1085 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001086 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001087 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001088 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
1089 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001090 if (getMemOffsetIsReg()) {
1091 OS << " offset:<register " << getMemOffsetRegNum();
1092 if (getMemOffsetRegShifted()) {
1093 OS << " offset-shift-type:" << getMemShiftType();
1094 OS << " offset-shift-amount:" << *getMemShiftAmount();
1095 }
1096 } else {
1097 OS << " offset:" << *getMemOffset();
1098 }
1099 if (getMemOffsetIsReg())
1100 OS << " (offset-is-reg)";
1101 if (getMemPreindexed())
1102 OS << " (pre-indexed)";
1103 if (getMemPostindexed())
1104 OS << " (post-indexed)";
1105 if (getMemNegative())
1106 OS << " (negative)";
1107 if (getMemWriteback())
1108 OS << " (writeback)";
1109 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001110 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001111 case ProcIFlags: {
1112 OS << "<ARM_PROC::";
1113 unsigned IFlags = getProcIFlags();
1114 for (int i=2; i >= 0; --i)
1115 if (IFlags & (1 << i))
1116 OS << ARM_PROC::IFlagsToString(1 << i);
1117 OS << ">";
1118 break;
1119 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001120 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +00001121 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001122 break;
Owen Anderson00828302011-03-18 22:50:18 +00001123 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +00001124 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
1125 break;
1126 case ShiftedRegister:
Owen Anderson92a20222011-07-21 18:54:16 +00001127 OS << "<so_reg_reg "
Jim Grosbache8606dc2011-07-13 17:50:29 +00001128 << ShiftedReg.SrcReg
1129 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
1130 << ", " << ShiftedReg.ShiftReg << ", "
1131 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
1132 << ">";
Owen Anderson00828302011-03-18 22:50:18 +00001133 break;
Owen Anderson92a20222011-07-21 18:54:16 +00001134 case ShiftedImmediate:
1135 OS << "<so_reg_imm "
1136 << ShiftedImm.SrcReg
1137 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedImm.ShiftImm))
1138 << ", " << ARM_AM::getSORegOffset(ShiftedImm.ShiftImm)
1139 << ">";
1140 break;
Bill Wendling0f630752010-11-17 04:32:08 +00001141 case RegisterList:
1142 case DPRRegisterList:
1143 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00001144 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001145
Bill Wendling5fa22a12010-11-09 23:28:44 +00001146 const SmallVectorImpl<unsigned> &RegList = getRegList();
1147 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001148 I = RegList.begin(), E = RegList.end(); I != E; ) {
1149 OS << *I;
1150 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001151 }
1152
1153 OS << ">";
1154 break;
1155 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001156 case Token:
1157 OS << "'" << getToken() << "'";
1158 break;
1159 }
1160}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001161
1162/// @name Auto-generated Match Functions
1163/// {
1164
1165static unsigned MatchRegisterName(StringRef Name);
1166
1167/// }
1168
Bob Wilson69df7232011-02-03 21:46:10 +00001169bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1170 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +00001171 RegNo = TryParseRegister();
1172
1173 return (RegNo == (unsigned)-1);
1174}
1175
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001176/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00001177/// and if it is a register name the token is eaten and the register number is
1178/// returned. Otherwise return -1.
1179///
1180int ARMAsmParser::TryParseRegister() {
1181 const AsmToken &Tok = Parser.getTok();
1182 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001183
Chris Lattnere5658fa2010-10-30 04:09:10 +00001184 // FIXME: Validate register for the current architecture; we have to do
1185 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001186 std::string upperCase = Tok.getString().str();
1187 std::string lowerCase = LowercaseString(upperCase);
1188 unsigned RegNum = MatchRegisterName(lowerCase);
1189 if (!RegNum) {
1190 RegNum = StringSwitch<unsigned>(lowerCase)
1191 .Case("r13", ARM::SP)
1192 .Case("r14", ARM::LR)
1193 .Case("r15", ARM::PC)
1194 .Case("ip", ARM::R12)
1195 .Default(0);
1196 }
1197 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001198
Chris Lattnere5658fa2010-10-30 04:09:10 +00001199 Parser.Lex(); // Eat identifier token.
1200 return RegNum;
1201}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001202
Jim Grosbach19906722011-07-13 18:49:30 +00001203// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1204// If a recoverable error occurs, return 1. If an irrecoverable error
1205// occurs, return -1. An irrecoverable error is one where tokens have been
1206// consumed in the process of trying to parse the shifter (i.e., when it is
1207// indeed a shifter operand, but malformed).
1208int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001209 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1210 SMLoc S = Parser.getTok().getLoc();
1211 const AsmToken &Tok = Parser.getTok();
1212 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1213
1214 std::string upperCase = Tok.getString().str();
1215 std::string lowerCase = LowercaseString(upperCase);
1216 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1217 .Case("lsl", ARM_AM::lsl)
1218 .Case("lsr", ARM_AM::lsr)
1219 .Case("asr", ARM_AM::asr)
1220 .Case("ror", ARM_AM::ror)
1221 .Case("rrx", ARM_AM::rrx)
1222 .Default(ARM_AM::no_shift);
1223
1224 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001225 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001226
Jim Grosbache8606dc2011-07-13 17:50:29 +00001227 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001228
Jim Grosbache8606dc2011-07-13 17:50:29 +00001229 // The source register for the shift has already been added to the
1230 // operand list, so we need to pop it off and combine it into the shifted
1231 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00001232 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00001233 if (!PrevOp->isReg())
1234 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1235 int SrcReg = PrevOp->getReg();
1236 int64_t Imm = 0;
1237 int ShiftReg = 0;
1238 if (ShiftTy == ARM_AM::rrx) {
1239 // RRX Doesn't have an explicit shift amount. The encoder expects
1240 // the shift register to be the same as the source register. Seems odd,
1241 // but OK.
1242 ShiftReg = SrcReg;
1243 } else {
1244 // Figure out if this is shifted by a constant or a register (for non-RRX).
1245 if (Parser.getTok().is(AsmToken::Hash)) {
1246 Parser.Lex(); // Eat hash.
1247 SMLoc ImmLoc = Parser.getTok().getLoc();
1248 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001249 if (getParser().ParseExpression(ShiftExpr)) {
1250 Error(ImmLoc, "invalid immediate shift value");
1251 return -1;
1252 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001253 // The expression must be evaluatable as an immediate.
1254 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001255 if (!CE) {
1256 Error(ImmLoc, "invalid immediate shift value");
1257 return -1;
1258 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001259 // Range check the immediate.
1260 // lsl, ror: 0 <= imm <= 31
1261 // lsr, asr: 0 <= imm <= 32
1262 Imm = CE->getValue();
1263 if (Imm < 0 ||
1264 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1265 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001266 Error(ImmLoc, "immediate shift value out of range");
1267 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001268 }
1269 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1270 ShiftReg = TryParseRegister();
1271 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001272 if (ShiftReg == -1) {
1273 Error (L, "expected immediate or register in shift operand");
1274 return -1;
1275 }
1276 } else {
1277 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001278 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001279 return -1;
1280 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001281 }
1282
Owen Anderson92a20222011-07-21 18:54:16 +00001283 if (ShiftReg && ShiftTy != ARM_AM::rrx)
1284 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
Jim Grosbache8606dc2011-07-13 17:50:29 +00001285 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001286 S, Parser.getTok().getLoc()));
Owen Anderson92a20222011-07-21 18:54:16 +00001287 else
1288 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
1289 S, Parser.getTok().getLoc()));
Owen Anderson00828302011-03-18 22:50:18 +00001290
Jim Grosbach19906722011-07-13 18:49:30 +00001291 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001292}
1293
1294
Bill Wendling50d0f582010-11-18 23:43:05 +00001295/// Try to parse a register name. The token must be an Identifier when called.
1296/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1297/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001298///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001299/// TODO this is likely to change to allow different register types and or to
1300/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001301bool ARMAsmParser::
1302TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001303 SMLoc S = Parser.getTok().getLoc();
1304 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001305 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001306 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001307
Bill Wendling50d0f582010-11-18 23:43:05 +00001308 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001309
Chris Lattnere5658fa2010-10-30 04:09:10 +00001310 const AsmToken &ExclaimTok = Parser.getTok();
1311 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001312 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1313 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001314 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001315 }
1316
Bill Wendling50d0f582010-11-18 23:43:05 +00001317 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001318}
1319
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001320/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1321/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1322/// "c5", ...
1323static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001324 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1325 // but efficient.
1326 switch (Name.size()) {
1327 default: break;
1328 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001329 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001330 return -1;
1331 switch (Name[1]) {
1332 default: return -1;
1333 case '0': return 0;
1334 case '1': return 1;
1335 case '2': return 2;
1336 case '3': return 3;
1337 case '4': return 4;
1338 case '5': return 5;
1339 case '6': return 6;
1340 case '7': return 7;
1341 case '8': return 8;
1342 case '9': return 9;
1343 }
1344 break;
1345 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001346 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001347 return -1;
1348 switch (Name[2]) {
1349 default: return -1;
1350 case '0': return 10;
1351 case '1': return 11;
1352 case '2': return 12;
1353 case '3': return 13;
1354 case '4': return 14;
1355 case '5': return 15;
1356 }
1357 break;
1358 }
1359
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001360 return -1;
1361}
1362
Jim Grosbachf922c472011-02-12 01:34:40 +00001363/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001364/// token must be an Identifier when called, and if it is a coprocessor
1365/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001366ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1367tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001368 SMLoc S = Parser.getTok().getLoc();
1369 const AsmToken &Tok = Parser.getTok();
1370 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1371
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001372 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001373 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001374 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001375
1376 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001377 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001378 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001379}
1380
Jim Grosbachf922c472011-02-12 01:34:40 +00001381/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001382/// token must be an Identifier when called, and if it is a coprocessor
1383/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001384ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1385tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001386 SMLoc S = Parser.getTok().getLoc();
1387 const AsmToken &Tok = Parser.getTok();
1388 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1389
1390 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1391 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001392 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001393
1394 Parser.Lex(); // Eat identifier token.
1395 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001396 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001397}
1398
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001399/// Parse a register list, return it if successful else return null. The first
1400/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001401bool ARMAsmParser::
1402ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001403 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001404 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001405 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001406
Bill Wendling7729e062010-11-09 22:44:22 +00001407 // Read the rest of the registers in the list.
1408 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001409 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001410
Bill Wendling7729e062010-11-09 22:44:22 +00001411 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001412 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001413 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001414
Sean Callanan18b83232010-01-19 21:44:56 +00001415 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001416 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001417 if (RegTok.isNot(AsmToken::Identifier)) {
1418 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001419 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001420 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001421
Bill Wendling1d6a2652010-11-06 10:40:24 +00001422 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001423 if (RegNum == -1) {
1424 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001425 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001426 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001427
Bill Wendlinge7176102010-11-06 22:36:58 +00001428 if (IsRange) {
1429 int Reg = PrevRegNum;
1430 do {
1431 ++Reg;
1432 Registers.push_back(std::make_pair(Reg, RegLoc));
1433 } while (Reg != RegNum);
1434 } else {
1435 Registers.push_back(std::make_pair(RegNum, RegLoc));
1436 }
1437
1438 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001439 } while (Parser.getTok().is(AsmToken::Comma) ||
1440 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001441
1442 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001443 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001444 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1445 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001446 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001447 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001448
Bill Wendlinge7176102010-11-06 22:36:58 +00001449 SMLoc E = RCurlyTok.getLoc();
1450 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001451
Bill Wendlinge7176102010-11-06 22:36:58 +00001452 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001453 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001454 RI = Registers.begin(), RE = Registers.end();
1455
Bill Wendling7caebff2011-01-12 21:20:59 +00001456 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001457 bool EmittedWarning = false;
1458
Bill Wendling7caebff2011-01-12 21:20:59 +00001459 DenseMap<unsigned, bool> RegMap;
1460 RegMap[HighRegNum] = true;
1461
Bill Wendlinge7176102010-11-06 22:36:58 +00001462 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001463 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001464 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001465
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001466 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001467 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001468 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001469 }
1470
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001471 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001472 Warning(RegInfo.second,
1473 "register not in ascending order in register list");
1474
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001475 RegMap[Reg] = true;
1476 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001477 }
1478
Bill Wendling50d0f582010-11-18 23:43:05 +00001479 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1480 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001481}
1482
Jim Grosbachf922c472011-02-12 01:34:40 +00001483/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1484ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1485tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001486 SMLoc S = Parser.getTok().getLoc();
1487 const AsmToken &Tok = Parser.getTok();
1488 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1489 StringRef OptStr = Tok.getString();
1490
1491 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1492 .Case("sy", ARM_MB::SY)
1493 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001494 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001495 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001496 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001497 .Case("ishst", ARM_MB::ISHST)
1498 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001499 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001500 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001501 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001502 .Case("osh", ARM_MB::OSH)
1503 .Case("oshst", ARM_MB::OSHST)
1504 .Default(~0U);
1505
1506 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001507 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001508
1509 Parser.Lex(); // Eat identifier token.
1510 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001511 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001512}
1513
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001514/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001515ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1516tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1517 SMLoc S = Parser.getTok().getLoc();
1518 const AsmToken &Tok = Parser.getTok();
1519 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1520 StringRef IFlagsStr = Tok.getString();
1521
1522 unsigned IFlags = 0;
1523 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1524 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1525 .Case("a", ARM_PROC::A)
1526 .Case("i", ARM_PROC::I)
1527 .Case("f", ARM_PROC::F)
1528 .Default(~0U);
1529
1530 // If some specific iflag is already set, it means that some letter is
1531 // present more than once, this is not acceptable.
1532 if (Flag == ~0U || (IFlags & Flag))
1533 return MatchOperand_NoMatch;
1534
1535 IFlags |= Flag;
1536 }
1537
1538 Parser.Lex(); // Eat identifier token.
1539 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1540 return MatchOperand_Success;
1541}
1542
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001543/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1544ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1545tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1546 SMLoc S = Parser.getTok().getLoc();
1547 const AsmToken &Tok = Parser.getTok();
1548 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1549 StringRef Mask = Tok.getString();
1550
1551 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1552 size_t Start = 0, Next = Mask.find('_');
1553 StringRef Flags = "";
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00001554 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001555 if (Next != StringRef::npos)
1556 Flags = Mask.slice(Next+1, Mask.size());
1557
1558 // FlagsVal contains the complete mask:
1559 // 3-0: Mask
1560 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1561 unsigned FlagsVal = 0;
1562
1563 if (SpecReg == "apsr") {
1564 FlagsVal = StringSwitch<unsigned>(Flags)
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00001565 .Case("nzcvq", 0x8) // same as CPSR_f
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001566 .Case("g", 0x4) // same as CPSR_s
1567 .Case("nzcvqg", 0xc) // same as CPSR_fs
1568 .Default(~0U);
1569
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001570 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001571 if (!Flags.empty())
1572 return MatchOperand_NoMatch;
1573 else
1574 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001575 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001576 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001577 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1578 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001579 for (int i = 0, e = Flags.size(); i != e; ++i) {
1580 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1581 .Case("c", 1)
1582 .Case("x", 2)
1583 .Case("s", 4)
1584 .Case("f", 8)
1585 .Default(~0U);
1586
1587 // If some specific flag is already set, it means that some letter is
1588 // present more than once, this is not acceptable.
1589 if (FlagsVal == ~0U || (FlagsVal & Flag))
1590 return MatchOperand_NoMatch;
1591 FlagsVal |= Flag;
1592 }
1593 } else // No match for special register.
1594 return MatchOperand_NoMatch;
1595
1596 // Special register without flags are equivalent to "fc" flags.
1597 if (!FlagsVal)
1598 FlagsVal = 0x9;
1599
1600 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1601 if (SpecReg == "spsr")
1602 FlagsVal |= 16;
1603
1604 Parser.Lex(); // Eat identifier token.
1605 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1606 return MatchOperand_Success;
1607}
1608
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001609/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1610ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1611tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001612 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001613
1614 if (ParseMemory(Operands, ARMII::AddrMode2))
1615 return MatchOperand_NoMatch;
1616
1617 return MatchOperand_Success;
1618}
1619
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001620/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1621ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1622tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1623 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1624
1625 if (ParseMemory(Operands, ARMII::AddrMode3))
1626 return MatchOperand_NoMatch;
1627
1628 return MatchOperand_Success;
1629}
1630
Jim Grosbachf6c05252011-07-21 17:23:04 +00001631ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1632parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
1633 int Low, int High) {
1634 const AsmToken &Tok = Parser.getTok();
1635 if (Tok.isNot(AsmToken::Identifier)) {
1636 Error(Parser.getTok().getLoc(), Op + " operand expected.");
1637 return MatchOperand_ParseFail;
1638 }
1639 StringRef ShiftName = Tok.getString();
1640 std::string LowerOp = LowercaseString(Op);
1641 std::string UpperOp = UppercaseString(Op);
1642 if (ShiftName != LowerOp && ShiftName != UpperOp) {
1643 Error(Parser.getTok().getLoc(), Op + " operand expected.");
1644 return MatchOperand_ParseFail;
1645 }
1646 Parser.Lex(); // Eat shift type token.
1647
1648 // There must be a '#' and a shift amount.
1649 if (Parser.getTok().isNot(AsmToken::Hash)) {
1650 Error(Parser.getTok().getLoc(), "'#' expected");
1651 return MatchOperand_ParseFail;
1652 }
1653 Parser.Lex(); // Eat hash token.
1654
1655 const MCExpr *ShiftAmount;
1656 SMLoc Loc = Parser.getTok().getLoc();
1657 if (getParser().ParseExpression(ShiftAmount)) {
1658 Error(Loc, "illegal expression");
1659 return MatchOperand_ParseFail;
1660 }
1661 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
1662 if (!CE) {
1663 Error(Loc, "constant expression expected");
1664 return MatchOperand_ParseFail;
1665 }
1666 int Val = CE->getValue();
1667 if (Val < Low || Val > High) {
1668 Error(Loc, "immediate value out of range");
1669 return MatchOperand_ParseFail;
1670 }
1671
1672 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
1673
1674 return MatchOperand_Success;
1675}
1676
Jim Grosbachc27d4f92011-07-22 17:44:50 +00001677ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1678parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1679 const AsmToken &Tok = Parser.getTok();
1680 SMLoc S = Tok.getLoc();
1681 if (Tok.isNot(AsmToken::Identifier)) {
1682 Error(Tok.getLoc(), "'be' or 'le' operand expected");
1683 return MatchOperand_ParseFail;
1684 }
1685 int Val = StringSwitch<int>(Tok.getString())
1686 .Case("be", 1)
1687 .Case("le", 0)
1688 .Default(-1);
1689 Parser.Lex(); // Eat the token.
1690
1691 if (Val == -1) {
1692 Error(Tok.getLoc(), "'be' or 'le' operand expected");
1693 return MatchOperand_ParseFail;
1694 }
1695 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
1696 getContext()),
1697 S, Parser.getTok().getLoc()));
1698 return MatchOperand_Success;
1699}
1700
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001701/// CvtLdWriteBackRegAddrMode2 - 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::
1705CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1706 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1707 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1708
1709 // Create a writeback register dummy placeholder.
1710 Inst.addOperand(MCOperand::CreateImm(0));
1711
1712 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1713 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1714 return true;
1715}
1716
1717/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1718/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1719/// when they refer multiple MIOperands inside a single one.
1720bool ARMAsmParser::
1721CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1722 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1723 // Create a writeback register dummy placeholder.
1724 Inst.addOperand(MCOperand::CreateImm(0));
1725 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1726 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1727 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1728 return true;
1729}
1730
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001731/// CvtLdWriteBackRegAddrMode3 - 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::
1735CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1736 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1737 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1738
1739 // Create a writeback register dummy placeholder.
1740 Inst.addOperand(MCOperand::CreateImm(0));
1741
1742 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1743 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1744 return true;
1745}
1746
1747/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1748/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1749/// when they refer multiple MIOperands inside a single one.
1750bool ARMAsmParser::
1751CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1752 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1753 // Create a writeback register dummy placeholder.
1754 Inst.addOperand(MCOperand::CreateImm(0));
1755 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1756 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1757 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1758 return true;
1759}
1760
Bill Wendlinge7176102010-11-06 22:36:58 +00001761/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001762/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001763///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001764/// TODO Only preindexing and postindexing addressing are started, unindexed
1765/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001766bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001767ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1768 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001769 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001770 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001771 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001772 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001773 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001774
Sean Callanan18b83232010-01-19 21:44:56 +00001775 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001776 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1777 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001778 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001779 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001780 int BaseRegNum = TryParseRegister();
1781 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001782 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001783 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001784 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001785
Daniel Dunbar05710932011-01-18 05:34:17 +00001786 // The next token must either be a comma or a closing bracket.
1787 const AsmToken &Tok = Parser.getTok();
1788 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1789 return true;
1790
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001791 bool Preindexed = false;
1792 bool Postindexed = false;
1793 bool OffsetIsReg = false;
1794 bool Negative = false;
1795 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001796 ARMOperand *WBOp = 0;
1797 int OffsetRegNum = -1;
1798 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001799 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001800 const MCExpr *ShiftAmount = 0;
1801 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001802
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001803 // First look for preindexed address forms, that is after the "[Rn" we now
1804 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001805 if (Tok.is(AsmToken::Comma)) {
1806 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001807 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001808
Chris Lattner550276e2010-10-28 20:52:15 +00001809 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1810 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001811 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001812 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001813 if (RBracTok.isNot(AsmToken::RBrac)) {
1814 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001815 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001816 }
Sean Callanan76264762010-04-02 22:27:05 +00001817 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001818 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001819
Sean Callanan18b83232010-01-19 21:44:56 +00001820 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001821 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001822 // None of addrmode3 instruction uses "!"
1823 if (AddrMode == ARMII::AddrMode3)
1824 return true;
1825
Bill Wendling50d0f582010-11-18 23:43:05 +00001826 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1827 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001828 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001829 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001830 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1831 if (AddrMode == ARMII::AddrMode2)
1832 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001833 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001834 } else {
1835 // The "[Rn" we have so far was not followed by a comma.
1836
Jim Grosbach80eb2332010-10-29 17:41:25 +00001837 // If there's anything other than the right brace, this is a post indexing
1838 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001839 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001840 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001841
Sean Callanan18b83232010-01-19 21:44:56 +00001842 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001843
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001844 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001845 Postindexed = true;
1846 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001847
Chris Lattner550276e2010-10-28 20:52:15 +00001848 if (NextTok.isNot(AsmToken::Comma)) {
1849 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001850 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001851 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001852
Sean Callananb9a25b72010-01-19 20:27:46 +00001853 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001854
Chris Lattner550276e2010-10-28 20:52:15 +00001855 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001856 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001857 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001858 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001859 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001860 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001861
1862 // Force Offset to exist if used.
1863 if (!OffsetIsReg) {
1864 if (!Offset)
1865 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001866 } else {
1867 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1868 Error(E, "shift amount not supported");
1869 return true;
1870 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001871 }
1872
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001873 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1874 Offset, OffsetRegNum, OffsetRegShifted,
1875 ShiftType, ShiftAmount, Preindexed,
1876 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001877 if (WBOp)
1878 Operands.push_back(WBOp);
1879
1880 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001881}
1882
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001883/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1884/// we will parse the following (were +/- means that a plus or minus is
1885/// optional):
1886/// +/-Rm
1887/// +/-Rm, shift
1888/// #offset
1889/// we return false on success or an error otherwise.
1890bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001891 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001892 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001893 const MCExpr *&ShiftAmount,
1894 const MCExpr *&Offset,
1895 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001896 int &OffsetRegNum,
1897 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001898 Negative = false;
1899 OffsetRegShifted = false;
1900 OffsetIsReg = false;
1901 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001902 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001903 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001904 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001905 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001906 else if (NextTok.is(AsmToken::Minus)) {
1907 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001908 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001909 }
1910 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001911 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001912 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001913 SMLoc CurLoc = OffsetRegTok.getLoc();
1914 OffsetRegNum = TryParseRegister();
1915 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001916 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001917 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001918 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001919 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001920
Bill Wendling12f40e92010-11-06 10:51:53 +00001921 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001922 if (OffsetRegNum != -1) {
1923 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001924 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001925 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001926 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001927
Sean Callanan18b83232010-01-19 21:44:56 +00001928 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001929 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001930 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001931 OffsetRegShifted = true;
1932 }
1933 }
1934 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1935 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001936 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001937 if (HashTok.isNot(AsmToken::Hash))
1938 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001939
Sean Callananb9a25b72010-01-19 20:27:46 +00001940 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001941
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001942 if (getParser().ParseExpression(Offset))
1943 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001944 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001945 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001946 return false;
1947}
1948
1949/// ParseShift as one of these two:
1950/// ( lsl | lsr | asr | ror ) , # shift_amount
1951/// rrx
1952/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001953bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1954 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001955 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001956 if (Tok.isNot(AsmToken::Identifier))
1957 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001958 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001959 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001960 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001961 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001962 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001963 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001964 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001965 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001966 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001967 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001968 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001969 else
1970 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001971 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001972
1973 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001974 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001975 return false;
1976
1977 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001978 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001979 if (HashTok.isNot(AsmToken::Hash))
1980 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001981 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001982
1983 if (getParser().ParseExpression(ShiftAmount))
1984 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001985
1986 return false;
1987}
1988
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001989/// Parse a arm instruction operand. For now this parses the operand regardless
1990/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001991bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001992 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001993 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001994
1995 // Check if the current operand has a custom associated parser, if so, try to
1996 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001997 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1998 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001999 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00002000 // If there wasn't a custom match, try the generic matcher below. Otherwise,
2001 // there was a match, but an error occurred, in which case, just return that
2002 // the operand parsing failed.
2003 if (ResTy == MatchOperand_ParseFail)
2004 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002005
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002006 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00002007 default:
2008 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00002009 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00002010 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00002011 if (!TryParseRegisterWithWriteBack(Operands))
2012 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00002013 int Res = TryParseShiftRegister(Operands);
2014 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00002015 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00002016 else if (Res == -1) // irrecoverable error
2017 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002018
2019 // Fall though for the Identifier case that is not a register or a
2020 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00002021 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00002022 case AsmToken::Integer: // things like 1f and 2b as a branch targets
2023 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00002024 // This was not a register so parse other operands that start with an
2025 // identifier (like labels) as expressions and create them as immediates.
2026 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00002027 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00002028 if (getParser().ParseExpression(IdVal))
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(IdVal, S, E));
2032 return false;
2033 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002034 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00002035 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002036 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00002037 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002038 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00002039 // #42 -> immediate.
2040 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00002041 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00002042 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002043 const MCExpr *ImmVal;
2044 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00002045 return true;
Sean Callanan76264762010-04-02 22:27:05 +00002046 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00002047 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
2048 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002049 case AsmToken::Colon: {
2050 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00002051 // FIXME: Check it's an expression prefix,
2052 // e.g. (FOO - :lower16:BAR) isn't legal.
2053 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002054 if (ParsePrefix(RefKind))
2055 return true;
2056
Evan Cheng75972122011-01-13 07:58:56 +00002057 const MCExpr *SubExprVal;
2058 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00002059 return true;
2060
Evan Cheng75972122011-01-13 07:58:56 +00002061 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
2062 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00002063 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00002064 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00002065 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002066 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00002067 }
2068}
2069
Evan Cheng75972122011-01-13 07:58:56 +00002070// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
2071// :lower16: and :upper16:.
2072bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
2073 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002074
2075 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00002076 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00002077 Parser.Lex(); // Eat ':'
2078
2079 if (getLexer().isNot(AsmToken::Identifier)) {
2080 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
2081 return true;
2082 }
2083
2084 StringRef IDVal = Parser.getTok().getIdentifier();
2085 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00002086 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002087 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00002088 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002089 } else {
2090 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
2091 return true;
2092 }
2093 Parser.Lex();
2094
2095 if (getLexer().isNot(AsmToken::Colon)) {
2096 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
2097 return true;
2098 }
2099 Parser.Lex(); // Eat the last ':'
2100 return false;
2101}
2102
2103const MCExpr *
2104ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
2105 MCSymbolRefExpr::VariantKind Variant) {
2106 // Recurse over the given expression, rebuilding it to apply the given variant
2107 // to the leftmost symbol.
2108 if (Variant == MCSymbolRefExpr::VK_None)
2109 return E;
2110
2111 switch (E->getKind()) {
2112 case MCExpr::Target:
2113 llvm_unreachable("Can't handle target expr yet");
2114 case MCExpr::Constant:
2115 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
2116
2117 case MCExpr::SymbolRef: {
2118 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
2119
2120 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
2121 return 0;
2122
2123 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
2124 }
2125
2126 case MCExpr::Unary:
2127 llvm_unreachable("Can't handle unary expressions yet");
2128
2129 case MCExpr::Binary: {
2130 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
2131 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
2132 const MCExpr *RHS = BE->getRHS();
2133 if (!LHS)
2134 return 0;
2135
2136 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
2137 }
2138 }
2139
2140 assert(0 && "Invalid expression kind!");
2141 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002142}
2143
Daniel Dunbar352e1482011-01-11 15:59:50 +00002144/// \brief Given a mnemonic, split out possible predication code and carry
2145/// setting letters to form a canonical mnemonic and flags.
2146//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002147// FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00002148StringRef ARMAsmParser::SplitMnemonic(StringRef Mnemonic,
2149 unsigned &PredicationCode,
2150 bool &CarrySetting,
2151 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00002152 PredicationCode = ARMCC::AL;
2153 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002154 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002155
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002156 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00002157 //
2158 // FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00002159 if ((Mnemonic == "movs" && isThumb()) ||
2160 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
2161 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
2162 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
2163 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
2164 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
2165 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
2166 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
Daniel Dunbar352e1482011-01-11 15:59:50 +00002167 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00002168
Jim Grosbach3f00e312011-07-11 17:09:57 +00002169 // First, split out any predication code. Ignore mnemonics we know aren't
2170 // predicated but do have a carry-set and so weren't caught above.
Jim Grosbachab40f4b2011-07-20 18:20:31 +00002171 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
Jim Grosbachbf2845c2011-07-22 22:06:05 +00002172 Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls") {
Jim Grosbach3f00e312011-07-11 17:09:57 +00002173 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
2174 .Case("eq", ARMCC::EQ)
2175 .Case("ne", ARMCC::NE)
2176 .Case("hs", ARMCC::HS)
2177 .Case("cs", ARMCC::HS)
2178 .Case("lo", ARMCC::LO)
2179 .Case("cc", ARMCC::LO)
2180 .Case("mi", ARMCC::MI)
2181 .Case("pl", ARMCC::PL)
2182 .Case("vs", ARMCC::VS)
2183 .Case("vc", ARMCC::VC)
2184 .Case("hi", ARMCC::HI)
2185 .Case("ls", ARMCC::LS)
2186 .Case("ge", ARMCC::GE)
2187 .Case("lt", ARMCC::LT)
2188 .Case("gt", ARMCC::GT)
2189 .Case("le", ARMCC::LE)
2190 .Case("al", ARMCC::AL)
2191 .Default(~0U);
2192 if (CC != ~0U) {
2193 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
2194 PredicationCode = CC;
2195 }
Bill Wendling52925b62010-10-29 23:50:21 +00002196 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002197
Daniel Dunbar352e1482011-01-11 15:59:50 +00002198 // Next, determine if we have a carry setting bit. We explicitly ignore all
2199 // the instructions we know end in 's'.
2200 if (Mnemonic.endswith("s") &&
2201 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002202 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
2203 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
2204 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
2205 Mnemonic == "vrsqrts" || (Mnemonic == "movs" && isThumb()))) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00002206 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
2207 CarrySetting = true;
2208 }
2209
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002210 // The "cps" instruction can have a interrupt mode operand which is glued into
2211 // the mnemonic. Check if this is the case, split it and parse the imod op
2212 if (Mnemonic.startswith("cps")) {
2213 // Split out any imod code.
2214 unsigned IMod =
2215 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
2216 .Case("ie", ARM_PROC::IE)
2217 .Case("id", ARM_PROC::ID)
2218 .Default(~0U);
2219 if (IMod != ~0U) {
2220 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2221 ProcessorIMod = IMod;
2222 }
2223 }
2224
Daniel Dunbar352e1482011-01-11 15:59:50 +00002225 return Mnemonic;
2226}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002227
2228/// \brief Given a canonical mnemonic, determine if the instruction ever allows
2229/// inclusion of carry set or predication code operands.
2230//
2231// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00002232void ARMAsmParser::
2233GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2234 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002235 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2236 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2237 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2238 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002239 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002240 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2241 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002242 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00002243 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002244 CanAcceptCarrySet = true;
2245 } else {
2246 CanAcceptCarrySet = false;
2247 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002248
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002249 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2250 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2251 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2252 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002253 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "clrex" ||
Jim Grosbachc27d4f92011-07-22 17:44:50 +00002254 Mnemonic == "setend" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002255 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumb())) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002256 CanAcceptPredicationCode = false;
2257 } else {
2258 CanAcceptPredicationCode = true;
2259 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002260
Evan Chengebdeeab2011-07-08 01:53:10 +00002261 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002262 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002263 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002264 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002265}
2266
2267/// Parse an arm instruction mnemonic followed by its operands.
2268bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2269 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2270 // Create the leading tokens for the mnemonic, split by '.' characters.
2271 size_t Start = 0, Next = Name.find('.');
Jim Grosbachffa32252011-07-19 19:13:28 +00002272 StringRef Mnemonic = Name.slice(Start, Next);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002273
Daniel Dunbar352e1482011-01-11 15:59:50 +00002274 // Split out the predication code and carry setting flag from the mnemonic.
2275 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002276 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002277 bool CarrySetting;
Jim Grosbachffa32252011-07-19 19:13:28 +00002278 Mnemonic = SplitMnemonic(Mnemonic, PredicationCode, CarrySetting,
Jim Grosbachc27d4f92011-07-22 17:44:50 +00002279 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002280
Jim Grosbachffa32252011-07-19 19:13:28 +00002281 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
2282
2283 // FIXME: This is all a pretty gross hack. We should automatically handle
2284 // optional operands like this via tblgen.
Bill Wendling9717fa92010-11-21 10:56:05 +00002285
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002286 // Next, add the CCOut and ConditionCode operands, if needed.
2287 //
2288 // For mnemonics which can ever incorporate a carry setting bit or predication
2289 // code, our matching model involves us always generating CCOut and
2290 // ConditionCode operands to match the mnemonic "as written" and then we let
2291 // the matcher deal with finding the right instruction or generating an
2292 // appropriate error.
2293 bool CanAcceptCarrySet, CanAcceptPredicationCode;
Jim Grosbachffa32252011-07-19 19:13:28 +00002294 GetMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002295
Jim Grosbach33c16a22011-07-14 22:04:21 +00002296 // If we had a carry-set on an instruction that can't do that, issue an
2297 // error.
2298 if (!CanAcceptCarrySet && CarrySetting) {
2299 Parser.EatToEndOfStatement();
Jim Grosbachffa32252011-07-19 19:13:28 +00002300 return Error(NameLoc, "instruction '" + Mnemonic +
Jim Grosbach33c16a22011-07-14 22:04:21 +00002301 "' can not set flags, but 's' suffix specified");
2302 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +00002303 // If we had a predication code on an instruction that can't do that, issue an
2304 // error.
2305 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
2306 Parser.EatToEndOfStatement();
2307 return Error(NameLoc, "instruction '" + Mnemonic +
2308 "' is not predicable, but condition code specified");
2309 }
Jim Grosbach33c16a22011-07-14 22:04:21 +00002310
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002311 // Add the carry setting operand, if necessary.
2312 //
2313 // FIXME: It would be awesome if we could somehow invent a location such that
2314 // match errors on this operand would print a nice diagnostic about how the
2315 // 's' character in the mnemonic resulted in a CCOut operand.
Jim Grosbach33c16a22011-07-14 22:04:21 +00002316 if (CanAcceptCarrySet)
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002317 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2318 NameLoc));
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002319
2320 // Add the predication code operand, if necessary.
2321 if (CanAcceptPredicationCode) {
2322 Operands.push_back(ARMOperand::CreateCondCode(
2323 ARMCC::CondCodes(PredicationCode), NameLoc));
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002324 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002325
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002326 // Add the processor imod operand, if necessary.
2327 if (ProcessorIMod) {
2328 Operands.push_back(ARMOperand::CreateImm(
2329 MCConstantExpr::Create(ProcessorIMod, getContext()),
2330 NameLoc, NameLoc));
2331 } else {
2332 // This mnemonic can't ever accept a imod, but the user wrote
2333 // one (or misspelled another mnemonic).
2334
2335 // FIXME: Issue a nice error.
2336 }
2337
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002338 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002339 while (Next != StringRef::npos) {
2340 Start = Next;
2341 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002342 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002343
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002344 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002345 }
2346
2347 // Read the remaining operands.
2348 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002349 // Read the first operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002350 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002351 Parser.EatToEndOfStatement();
2352 return true;
2353 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002354
2355 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002356 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002357
2358 // Parse and remember the operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002359 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002360 Parser.EatToEndOfStatement();
2361 return true;
2362 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002363 }
2364 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002365
Chris Lattnercbf8a982010-09-11 16:18:25 +00002366 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2367 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002368 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002369 }
Bill Wendling146018f2010-11-06 21:42:12 +00002370
Chris Lattner34e53142010-09-08 05:10:46 +00002371 Parser.Lex(); // Consume the EndOfStatement
Jim Grosbachffa32252011-07-19 19:13:28 +00002372
2373
2374 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
2375 // another does not. Specifically, the MOVW instruction does not. So we
2376 // special case it here and remove the defaulted (non-setting) cc_out
2377 // operand if that's the instruction we're trying to match.
2378 //
2379 // We do this post-processing of the explicit operands rather than just
2380 // conditionally adding the cc_out in the first place because we need
2381 // to check the type of the parsed immediate operand.
2382 if (Mnemonic == "mov" && Operands.size() > 4 &&
2383 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
Jim Grosbach731f2092011-07-19 19:45:44 +00002384 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
2385 static_cast<ARMOperand*>(Operands[1])->getReg() == 0) {
Jim Grosbachffa32252011-07-19 19:13:28 +00002386 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
2387 Operands.erase(Operands.begin() + 1);
2388 delete Op;
2389 }
2390
Chris Lattner98986712010-01-14 22:21:20 +00002391 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002392}
2393
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002394bool ARMAsmParser::
2395MatchAndEmitInstruction(SMLoc IDLoc,
2396 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2397 MCStreamer &Out) {
2398 MCInst Inst;
2399 unsigned ErrorInfo;
Jim Grosbach5a187002011-07-19 18:32:48 +00002400 MatchResultTy MatchResult;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002401 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002402 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002403 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002404 Out.EmitInstruction(Inst);
2405 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002406 case Match_MissingFeature:
2407 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2408 return true;
2409 case Match_InvalidOperand: {
2410 SMLoc ErrorLoc = IDLoc;
2411 if (ErrorInfo != ~0U) {
2412 if (ErrorInfo >= Operands.size())
2413 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002414
Chris Lattnere73d4f82010-10-28 21:41:58 +00002415 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2416 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2417 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002418
Chris Lattnere73d4f82010-10-28 21:41:58 +00002419 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002420 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002421 case Match_MnemonicFail:
2422 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002423 case Match_ConversionFail:
2424 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002425 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002426
Eric Christopherc223e2b2010-10-29 09:26:59 +00002427 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002428 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002429}
2430
Kevin Enderby515d5092009-10-15 20:48:48 +00002431/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002432bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2433 StringRef IDVal = DirectiveID.getIdentifier();
2434 if (IDVal == ".word")
2435 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002436 else if (IDVal == ".thumb")
2437 return ParseDirectiveThumb(DirectiveID.getLoc());
2438 else if (IDVal == ".thumb_func")
2439 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2440 else if (IDVal == ".code")
2441 return ParseDirectiveCode(DirectiveID.getLoc());
2442 else if (IDVal == ".syntax")
2443 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002444 return true;
2445}
2446
2447/// ParseDirectiveWord
2448/// ::= .word [ expression (, expression)* ]
2449bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2450 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2451 for (;;) {
2452 const MCExpr *Value;
2453 if (getParser().ParseExpression(Value))
2454 return true;
2455
Chris Lattneraaec2052010-01-19 19:46:13 +00002456 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002457
2458 if (getLexer().is(AsmToken::EndOfStatement))
2459 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002460
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002461 // FIXME: Improve diagnostic.
2462 if (getLexer().isNot(AsmToken::Comma))
2463 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002464 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002465 }
2466 }
2467
Sean Callananb9a25b72010-01-19 20:27:46 +00002468 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002469 return false;
2470}
2471
Kevin Enderby515d5092009-10-15 20:48:48 +00002472/// ParseDirectiveThumb
2473/// ::= .thumb
2474bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2475 if (getLexer().isNot(AsmToken::EndOfStatement))
2476 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002477 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002478
2479 // TODO: set thumb mode
2480 // TODO: tell the MC streamer the mode
2481 // getParser().getStreamer().Emit???();
2482 return false;
2483}
2484
2485/// ParseDirectiveThumbFunc
2486/// ::= .thumbfunc symbol_name
2487bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002488 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2489 bool isMachO = MAI.hasSubsectionsViaSymbols();
2490 StringRef Name;
2491
2492 // Darwin asm has function name after .thumb_func direction
2493 // ELF doesn't
2494 if (isMachO) {
2495 const AsmToken &Tok = Parser.getTok();
2496 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2497 return Error(L, "unexpected token in .thumb_func directive");
2498 Name = Tok.getString();
2499 Parser.Lex(); // Consume the identifier token.
2500 }
2501
Kevin Enderby515d5092009-10-15 20:48:48 +00002502 if (getLexer().isNot(AsmToken::EndOfStatement))
2503 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002504 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002505
Rafael Espindola64695402011-05-16 16:17:21 +00002506 // FIXME: assuming function name will be the line following .thumb_func
2507 if (!isMachO) {
2508 Name = Parser.getTok().getString();
2509 }
2510
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002511 // Mark symbol as a thumb symbol.
2512 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2513 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002514 return false;
2515}
2516
2517/// ParseDirectiveSyntax
2518/// ::= .syntax unified | divided
2519bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002520 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002521 if (Tok.isNot(AsmToken::Identifier))
2522 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002523 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002524 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002525 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002526 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002527 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002528 else
2529 return Error(L, "unrecognized syntax mode in .syntax directive");
2530
2531 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002532 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002533 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002534
2535 // TODO tell the MC streamer the mode
2536 // getParser().getStreamer().Emit???();
2537 return false;
2538}
2539
2540/// ParseDirectiveCode
2541/// ::= .code 16 | 32
2542bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002543 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002544 if (Tok.isNot(AsmToken::Integer))
2545 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002546 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002547 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002548 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002549 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002550 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002551 else
2552 return Error(L, "invalid operand to .code directive");
2553
2554 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002555 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002556 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002557
Evan Cheng32869202011-07-08 22:36:29 +00002558 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002559 if (!isThumb())
2560 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002561 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002562 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002563 if (isThumb())
2564 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002565 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002566 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002567
Kevin Enderby515d5092009-10-15 20:48:48 +00002568 return false;
2569}
2570
Sean Callanan90b70972010-04-07 20:29:34 +00002571extern "C" void LLVMInitializeARMAsmLexer();
2572
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002573/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002574extern "C" void LLVMInitializeARMAsmParser() {
2575 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2576 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002577 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002578}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002579
Chris Lattner0692ee62010-09-06 19:11:01 +00002580#define GET_REGISTER_MATCHER
2581#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002582#include "ARMGenAsmMatcher.inc"