blob: fd8f815207da0503ef5f40def1d699fe3da6af3c [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 }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000129
130 // Asm Match Converter Methods
131 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
132 const SmallVectorImpl<MCParsedAsmOperand*> &);
133 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
134 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000135 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
136 const SmallVectorImpl<MCParsedAsmOperand*> &);
137 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
138 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000139
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000140public:
Evan Chengffc0e732011-07-09 05:47:46 +0000141 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
142 : TargetAsmParser(), STI(_STI), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000143 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000144
Evan Chengebdeeab2011-07-08 01:53:10 +0000145 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000146 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evan Chengebdeeab2011-07-08 01:53:10 +0000147 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000148
Benjamin Kramer38e59892010-07-14 22:38:02 +0000149 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000150 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000151 virtual bool ParseDirective(AsmToken DirectiveID);
152};
Jim Grosbach16c74252010-10-29 14:46:02 +0000153} // end anonymous namespace
154
Chris Lattner3a697562010-10-28 17:20:03 +0000155namespace {
156
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000157/// ARMOperand - Instances of this class represent a parsed ARM machine
158/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000159class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000160 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000161 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000162 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000163 CoprocNum,
164 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000165 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000166 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000167 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000168 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000169 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000170 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000171 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000172 DPRRegisterList,
173 SPRRegisterList,
Jim Grosbache8606dc2011-07-13 17:50:29 +0000174 ShiftedRegister,
Owen Anderson92a20222011-07-21 18:54:16 +0000175 ShiftedImmediate,
Owen Anderson00828302011-03-18 22:50:18 +0000176 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000177 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000178 } Kind;
179
Sean Callanan76264762010-04-02 22:27:05 +0000180 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000181 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000182
183 union {
184 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000185 ARMCC::CondCodes Val;
186 } CC;
187
188 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000189 ARM_MB::MemBOpt Val;
190 } MBOpt;
191
192 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000193 unsigned Val;
194 } Cop;
195
196 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000197 ARM_PROC::IFlags Val;
198 } IFlags;
199
200 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000201 unsigned Val;
202 } MMask;
203
204 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000205 const char *Data;
206 unsigned Length;
207 } Tok;
208
209 struct {
210 unsigned RegNum;
211 } Reg;
212
Bill Wendling8155e5b2010-11-06 22:19:43 +0000213 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000214 const MCExpr *Val;
215 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000216
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000217 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000218 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000219 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000220 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000221 union {
222 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
223 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
224 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000225 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000226 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000227 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000228 unsigned Preindexed : 1;
229 unsigned Postindexed : 1;
230 unsigned OffsetIsReg : 1;
231 unsigned Negative : 1; // only used when OffsetIsReg is true
232 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000233 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000234
235 struct {
236 ARM_AM::ShiftOpc ShiftTy;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000237 unsigned Imm;
Owen Anderson00828302011-03-18 22:50:18 +0000238 } Shift;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000239 struct {
240 ARM_AM::ShiftOpc ShiftTy;
241 unsigned SrcReg;
242 unsigned ShiftReg;
243 unsigned ShiftImm;
244 } ShiftedReg;
Owen Anderson92a20222011-07-21 18:54:16 +0000245 struct {
246 ARM_AM::ShiftOpc ShiftTy;
247 unsigned SrcReg;
248 unsigned ShiftImm;
249 } ShiftedImm;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000250 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000251
Bill Wendling146018f2010-11-06 21:42:12 +0000252 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
253public:
Sean Callanan76264762010-04-02 22:27:05 +0000254 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
255 Kind = o.Kind;
256 StartLoc = o.StartLoc;
257 EndLoc = o.EndLoc;
258 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000259 case CondCode:
260 CC = o.CC;
261 break;
Sean Callanan76264762010-04-02 22:27:05 +0000262 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000263 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000264 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000265 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000266 case Register:
267 Reg = o.Reg;
268 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000269 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000270 case DPRRegisterList:
271 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000272 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000273 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000274 case CoprocNum:
275 case CoprocReg:
276 Cop = o.Cop;
277 break;
Sean Callanan76264762010-04-02 22:27:05 +0000278 case Immediate:
279 Imm = o.Imm;
280 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000281 case MemBarrierOpt:
282 MBOpt = o.MBOpt;
283 break;
Sean Callanan76264762010-04-02 22:27:05 +0000284 case Memory:
285 Mem = o.Mem;
286 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000287 case MSRMask:
288 MMask = o.MMask;
289 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000290 case ProcIFlags:
291 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000292 break;
293 case Shifter:
294 Shift = o.Shift;
295 break;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000296 case ShiftedRegister:
297 ShiftedReg = o.ShiftedReg;
298 break;
Owen Anderson92a20222011-07-21 18:54:16 +0000299 case ShiftedImmediate:
300 ShiftedImm = o.ShiftedImm;
301 break;
Sean Callanan76264762010-04-02 22:27:05 +0000302 }
303 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000304
Sean Callanan76264762010-04-02 22:27:05 +0000305 /// getStartLoc - Get the location of the first token of this operand.
306 SMLoc getStartLoc() const { return StartLoc; }
307 /// getEndLoc - Get the location of the last token of this operand.
308 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000309
Daniel Dunbar8462b302010-08-11 06:36:53 +0000310 ARMCC::CondCodes getCondCode() const {
311 assert(Kind == CondCode && "Invalid access!");
312 return CC.Val;
313 }
314
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000315 unsigned getCoproc() const {
316 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
317 return Cop.Val;
318 }
319
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000320 StringRef getToken() const {
321 assert(Kind == Token && "Invalid access!");
322 return StringRef(Tok.Data, Tok.Length);
323 }
324
325 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000326 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000327 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000328 }
329
Bill Wendling5fa22a12010-11-09 23:28:44 +0000330 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000331 assert((Kind == RegisterList || Kind == DPRRegisterList ||
332 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000333 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000334 }
335
Kevin Enderbycfe07242009-10-13 22:19:02 +0000336 const MCExpr *getImm() const {
337 assert(Kind == Immediate && "Invalid access!");
338 return Imm.Val;
339 }
340
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000341 ARM_MB::MemBOpt getMemBarrierOpt() const {
342 assert(Kind == MemBarrierOpt && "Invalid access!");
343 return MBOpt.Val;
344 }
345
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000346 ARM_PROC::IFlags getProcIFlags() const {
347 assert(Kind == ProcIFlags && "Invalid access!");
348 return IFlags.Val;
349 }
350
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000351 unsigned getMSRMask() const {
352 assert(Kind == MSRMask && "Invalid access!");
353 return MMask.Val;
354 }
355
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000356 /// @name Memory Operand Accessors
357 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000358 ARMII::AddrMode getMemAddrMode() const {
359 return Mem.AddrMode;
360 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000361 unsigned getMemBaseRegNum() const {
362 return Mem.BaseRegNum;
363 }
364 unsigned getMemOffsetRegNum() const {
365 assert(Mem.OffsetIsReg && "Invalid access!");
366 return Mem.Offset.RegNum;
367 }
368 const MCExpr *getMemOffset() const {
369 assert(!Mem.OffsetIsReg && "Invalid access!");
370 return Mem.Offset.Value;
371 }
372 unsigned getMemOffsetRegShifted() const {
373 assert(Mem.OffsetIsReg && "Invalid access!");
374 return Mem.OffsetRegShifted;
375 }
376 const MCExpr *getMemShiftAmount() const {
377 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
378 return Mem.ShiftAmount;
379 }
Owen Anderson00828302011-03-18 22:50:18 +0000380 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000381 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
382 return Mem.ShiftType;
383 }
384 bool getMemPreindexed() const { return Mem.Preindexed; }
385 bool getMemPostindexed() const { return Mem.Postindexed; }
386 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
387 bool getMemNegative() const { return Mem.Negative; }
388 bool getMemWriteback() const { return Mem.Writeback; }
389
390 /// @}
391
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000392 bool isCoprocNum() const { return Kind == CoprocNum; }
393 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000394 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000395 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000396 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000397 bool isImm0_255() const {
398 if (Kind != Immediate)
399 return false;
400 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
401 if (!CE) return false;
402 int64_t Value = CE->getValue();
403 return Value >= 0 && Value < 256;
404 }
Jim Grosbach83ab0702011-07-13 22:01:08 +0000405 bool isImm0_7() const {
406 if (Kind != Immediate)
407 return false;
408 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
409 if (!CE) return false;
410 int64_t Value = CE->getValue();
411 return Value >= 0 && Value < 8;
412 }
413 bool isImm0_15() const {
414 if (Kind != Immediate)
415 return false;
416 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
417 if (!CE) return false;
418 int64_t Value = CE->getValue();
419 return Value >= 0 && Value < 16;
420 }
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000421 bool isImm0_31() const {
422 if (Kind != Immediate)
423 return false;
424 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
425 if (!CE) return false;
426 int64_t Value = CE->getValue();
427 return Value >= 0 && Value < 32;
428 }
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000429 bool isImm0_65535() const {
430 if (Kind != Immediate)
431 return false;
432 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
433 if (!CE) return false;
434 int64_t Value = CE->getValue();
435 return Value >= 0 && Value < 65536;
436 }
Jim Grosbachffa32252011-07-19 19:13:28 +0000437 bool isImm0_65535Expr() const {
438 if (Kind != Immediate)
439 return false;
440 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
441 // If it's not a constant expression, it'll generate a fixup and be
442 // handled later.
443 if (!CE) return true;
444 int64_t Value = CE->getValue();
445 return Value >= 0 && Value < 65536;
446 }
Jim Grosbachf6c05252011-07-21 17:23:04 +0000447 bool isPKHLSLImm() const {
448 if (Kind != Immediate)
449 return false;
450 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
451 if (!CE) return false;
452 int64_t Value = CE->getValue();
453 return Value >= 0 && Value < 32;
454 }
455 bool isPKHASRImm() const {
456 if (Kind != Immediate)
457 return false;
458 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
459 if (!CE) return false;
460 int64_t Value = CE->getValue();
461 return Value > 0 && Value <= 32;
462 }
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000463 bool isARMSOImm() const {
464 if (Kind != Immediate)
465 return false;
466 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
467 if (!CE) return false;
468 int64_t Value = CE->getValue();
469 return ARM_AM::getSOImmVal(Value) != -1;
470 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000471 bool isT2SOImm() const {
472 if (Kind != Immediate)
473 return false;
474 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
475 if (!CE) return false;
476 int64_t Value = CE->getValue();
477 return ARM_AM::getT2SOImmVal(Value) != -1;
478 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000479 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000480 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000481 bool isDPRRegList() const { return Kind == DPRRegisterList; }
482 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000483 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000484 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000485 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000486 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000487 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Owen Anderson92a20222011-07-21 18:54:16 +0000488 bool isShiftedImm() const { return Kind == ShiftedImmediate; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000489 bool isMemMode2() const {
490 if (getMemAddrMode() != ARMII::AddrMode2)
491 return false;
492
493 if (getMemOffsetIsReg())
494 return true;
495
496 if (getMemNegative() &&
497 !(getMemPostindexed() || getMemPreindexed()))
498 return false;
499
500 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
501 if (!CE) return false;
502 int64_t Value = CE->getValue();
503
504 // The offset must be in the range 0-4095 (imm12).
505 if (Value > 4095 || Value < -4095)
506 return false;
507
508 return true;
509 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000510 bool isMemMode3() const {
511 if (getMemAddrMode() != ARMII::AddrMode3)
512 return false;
513
514 if (getMemOffsetIsReg()) {
515 if (getMemOffsetRegShifted())
516 return false; // No shift with offset reg allowed
517 return true;
518 }
519
520 if (getMemNegative() &&
521 !(getMemPostindexed() || getMemPreindexed()))
522 return false;
523
524 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
525 if (!CE) return false;
526 int64_t Value = CE->getValue();
527
528 // The offset must be in the range 0-255 (imm8).
529 if (Value > 255 || Value < -255)
530 return false;
531
532 return true;
533 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000534 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000535 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
536 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000537 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000538
Daniel Dunbar4b462672011-01-18 05:55:27 +0000539 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000540 if (!CE) return false;
541
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000542 // The offset must be a multiple of 4 in the range 0-1020.
543 int64_t Value = CE->getValue();
544 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
545 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000546 bool isMemMode7() const {
547 if (!isMemory() ||
548 getMemPreindexed() ||
549 getMemPostindexed() ||
550 getMemOffsetIsReg() ||
551 getMemNegative() ||
552 getMemWriteback())
553 return false;
554
555 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
556 if (!CE) return false;
557
558 if (CE->getValue())
559 return false;
560
561 return true;
562 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000563 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000564 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000565 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000566 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000567 }
568 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000569 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000570 return false;
571
Daniel Dunbar4b462672011-01-18 05:55:27 +0000572 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000573 if (!CE) return false;
574
575 // The offset must be a multiple of 4 in the range 0-124.
576 uint64_t Value = CE->getValue();
577 return ((Value & 0x3) == 0 && Value <= 124);
578 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000579 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000580 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000581
582 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000583 // Add as immediates when possible. Null MCExpr = 0.
584 if (Expr == 0)
585 Inst.addOperand(MCOperand::CreateImm(0));
586 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000587 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
588 else
589 Inst.addOperand(MCOperand::CreateExpr(Expr));
590 }
591
Daniel Dunbar8462b302010-08-11 06:36:53 +0000592 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000593 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000594 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000595 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
596 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000597 }
598
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000599 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
600 assert(N == 1 && "Invalid number of operands!");
601 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
602 }
603
604 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
605 assert(N == 1 && "Invalid number of operands!");
606 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
607 }
608
Jim Grosbachd67641b2010-12-06 18:21:12 +0000609 void addCCOutOperands(MCInst &Inst, unsigned N) const {
610 assert(N == 1 && "Invalid number of operands!");
611 Inst.addOperand(MCOperand::CreateReg(getReg()));
612 }
613
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000614 void addRegOperands(MCInst &Inst, unsigned N) const {
615 assert(N == 1 && "Invalid number of operands!");
616 Inst.addOperand(MCOperand::CreateReg(getReg()));
617 }
618
Jim Grosbache8606dc2011-07-13 17:50:29 +0000619 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
620 assert(N == 3 && "Invalid number of operands!");
621 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
Jim Grosbache8606dc2011-07-13 17:50:29 +0000622 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
623 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
624 Inst.addOperand(MCOperand::CreateImm(
625 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
626 }
627
Owen Anderson92a20222011-07-21 18:54:16 +0000628 void addShiftedImmOperands(MCInst &Inst, unsigned N) const {
Owen Anderson152d4a42011-07-21 23:38:37 +0000629 assert(N == 2 && "Invalid number of operands!");
Owen Anderson92a20222011-07-21 18:54:16 +0000630 assert(isShiftedImm() && "addShiftedImmOperands() on non ShiftedImm!");
631 Inst.addOperand(MCOperand::CreateReg(ShiftedImm.SrcReg));
Owen Anderson92a20222011-07-21 18:54:16 +0000632 Inst.addOperand(MCOperand::CreateImm(
633 ARM_AM::getSORegOpc(ShiftedImm.ShiftTy, ShiftedImm.ShiftImm)));
634 }
635
636
Owen Anderson00828302011-03-18 22:50:18 +0000637 void addShifterOperands(MCInst &Inst, unsigned N) const {
638 assert(N == 1 && "Invalid number of operands!");
639 Inst.addOperand(MCOperand::CreateImm(
640 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
641 }
642
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000643 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000644 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000645 const SmallVectorImpl<unsigned> &RegList = getRegList();
646 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000647 I = RegList.begin(), E = RegList.end(); I != E; ++I)
648 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000649 }
650
Bill Wendling0f630752010-11-17 04:32:08 +0000651 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
652 addRegListOperands(Inst, N);
653 }
654
655 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
656 addRegListOperands(Inst, N);
657 }
658
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000659 void addImmOperands(MCInst &Inst, unsigned N) const {
660 assert(N == 1 && "Invalid number of operands!");
661 addExpr(Inst, getImm());
662 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000663
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000664 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
665 assert(N == 1 && "Invalid number of operands!");
666 addExpr(Inst, getImm());
667 }
668
Jim Grosbach83ab0702011-07-13 22:01:08 +0000669 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
670 assert(N == 1 && "Invalid number of operands!");
671 addExpr(Inst, getImm());
672 }
673
674 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
675 assert(N == 1 && "Invalid number of operands!");
676 addExpr(Inst, getImm());
677 }
678
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000679 void addImm0_31Operands(MCInst &Inst, unsigned N) const {
680 assert(N == 1 && "Invalid number of operands!");
681 addExpr(Inst, getImm());
682 }
683
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000684 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
685 assert(N == 1 && "Invalid number of operands!");
686 addExpr(Inst, getImm());
687 }
688
Jim Grosbachffa32252011-07-19 19:13:28 +0000689 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
690 assert(N == 1 && "Invalid number of operands!");
691 addExpr(Inst, getImm());
692 }
693
Jim Grosbachf6c05252011-07-21 17:23:04 +0000694 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
695 assert(N == 1 && "Invalid number of operands!");
696 addExpr(Inst, getImm());
697 }
698
699 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
700 assert(N == 1 && "Invalid number of operands!");
701 // An ASR value of 32 encodes as 0, so that's how we want to add it to
702 // the instruction as well.
703 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
704 int Val = CE->getValue();
705 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
706 }
707
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000708 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
709 assert(N == 1 && "Invalid number of operands!");
710 addExpr(Inst, getImm());
711 }
712
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000713 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
714 assert(N == 1 && "Invalid number of operands!");
715 addExpr(Inst, getImm());
716 }
717
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000718 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
719 assert(N == 1 && "Invalid number of operands!");
720 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
721 }
722
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000723 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
724 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
725 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
726
727 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000728 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000729 assert((CE || CE->getValue() == 0) &&
730 "No offset operand support in mode 7");
731 }
732
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000733 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
734 assert(isMemMode2() && "Invalid mode or number of operands!");
735 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
736 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
737
738 if (getMemOffsetIsReg()) {
739 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
740
741 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
742 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
743 int64_t ShiftAmount = 0;
744
745 if (getMemOffsetRegShifted()) {
746 ShOpc = getMemShiftType();
747 const MCConstantExpr *CE =
748 dyn_cast<MCConstantExpr>(getMemShiftAmount());
749 ShiftAmount = CE->getValue();
750 }
751
752 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
753 ShOpc, IdxMode)));
754 return;
755 }
756
757 // Create a operand placeholder to always yield the same number of operands.
758 Inst.addOperand(MCOperand::CreateReg(0));
759
760 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
761 // the difference?
762 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
763 assert(CE && "Non-constant mode 2 offset operand!");
764 int64_t Offset = CE->getValue();
765
766 if (Offset >= 0)
767 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
768 Offset, ARM_AM::no_shift, IdxMode)));
769 else
770 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
771 -Offset, ARM_AM::no_shift, IdxMode)));
772 }
773
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000774 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
775 assert(isMemMode3() && "Invalid mode or number of operands!");
776 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
777 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
778
779 if (getMemOffsetIsReg()) {
780 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
781
782 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
783 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
784 IdxMode)));
785 return;
786 }
787
788 // Create a operand placeholder to always yield the same number of operands.
789 Inst.addOperand(MCOperand::CreateReg(0));
790
791 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
792 // the difference?
793 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
794 assert(CE && "Non-constant mode 3 offset operand!");
795 int64_t Offset = CE->getValue();
796
797 if (Offset >= 0)
798 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
799 Offset, IdxMode)));
800 else
801 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
802 -Offset, IdxMode)));
803 }
804
Chris Lattner14b93852010-10-29 00:27:31 +0000805 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
806 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000807
Daniel Dunbar4b462672011-01-18 05:55:27 +0000808 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
809 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000810
Jim Grosbach80eb2332010-10-29 17:41:25 +0000811 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
812 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000813 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000814 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000815
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000816 // The MCInst offset operand doesn't include the low two bits (like
817 // the instruction encoding).
818 int64_t Offset = CE->getValue() / 4;
819 if (Offset >= 0)
820 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
821 Offset)));
822 else
823 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
824 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000825 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000826
Bill Wendlingf4caf692010-12-14 03:36:38 +0000827 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
828 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000829 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
830 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000831 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000832
Bill Wendlingf4caf692010-12-14 03:36:38 +0000833 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
834 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000835 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
836 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000837 assert(CE && "Non-constant mode offset operand!");
838 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000839 }
840
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000841 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
842 assert(N == 1 && "Invalid number of operands!");
843 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
844 }
845
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000846 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
847 assert(N == 1 && "Invalid number of operands!");
848 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
849 }
850
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000851 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000852
Chris Lattner3a697562010-10-28 17:20:03 +0000853 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
854 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000855 Op->CC.Val = CC;
856 Op->StartLoc = S;
857 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000858 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000859 }
860
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000861 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
862 ARMOperand *Op = new ARMOperand(CoprocNum);
863 Op->Cop.Val = CopVal;
864 Op->StartLoc = S;
865 Op->EndLoc = S;
866 return Op;
867 }
868
869 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
870 ARMOperand *Op = new ARMOperand(CoprocReg);
871 Op->Cop.Val = CopVal;
872 Op->StartLoc = S;
873 Op->EndLoc = S;
874 return Op;
875 }
876
Jim Grosbachd67641b2010-12-06 18:21:12 +0000877 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
878 ARMOperand *Op = new ARMOperand(CCOut);
879 Op->Reg.RegNum = RegNum;
880 Op->StartLoc = S;
881 Op->EndLoc = S;
882 return Op;
883 }
884
Chris Lattner3a697562010-10-28 17:20:03 +0000885 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
886 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000887 Op->Tok.Data = Str.data();
888 Op->Tok.Length = Str.size();
889 Op->StartLoc = S;
890 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000891 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000892 }
893
Bill Wendling50d0f582010-11-18 23:43:05 +0000894 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000895 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000896 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000897 Op->StartLoc = S;
898 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000899 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000900 }
901
Jim Grosbache8606dc2011-07-13 17:50:29 +0000902 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
903 unsigned SrcReg,
904 unsigned ShiftReg,
905 unsigned ShiftImm,
906 SMLoc S, SMLoc E) {
907 ARMOperand *Op = new ARMOperand(ShiftedRegister);
908 Op->ShiftedReg.ShiftTy = ShTy;
909 Op->ShiftedReg.SrcReg = SrcReg;
910 Op->ShiftedReg.ShiftReg = ShiftReg;
911 Op->ShiftedReg.ShiftImm = ShiftImm;
912 Op->StartLoc = S;
913 Op->EndLoc = E;
914 return Op;
915 }
916
Owen Anderson92a20222011-07-21 18:54:16 +0000917 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
918 unsigned SrcReg,
919 unsigned ShiftImm,
920 SMLoc S, SMLoc E) {
921 ARMOperand *Op = new ARMOperand(ShiftedImmediate);
922 Op->ShiftedImm.ShiftTy = ShTy;
923 Op->ShiftedImm.SrcReg = SrcReg;
924 Op->ShiftedImm.ShiftImm = ShiftImm;
925 Op->StartLoc = S;
926 Op->EndLoc = E;
927 return Op;
928 }
929
Owen Anderson00828302011-03-18 22:50:18 +0000930 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
931 SMLoc S, SMLoc E) {
932 ARMOperand *Op = new ARMOperand(Shifter);
933 Op->Shift.ShiftTy = ShTy;
934 Op->StartLoc = S;
935 Op->EndLoc = E;
936 return Op;
937 }
938
Bill Wendling7729e062010-11-09 22:44:22 +0000939 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000940 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000941 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000942 KindTy Kind = RegisterList;
943
944 if (ARM::DPRRegClass.contains(Regs.front().first))
945 Kind = DPRRegisterList;
946 else if (ARM::SPRRegClass.contains(Regs.front().first))
947 Kind = SPRRegisterList;
948
949 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000950 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000951 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000952 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000953 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000954 Op->StartLoc = StartLoc;
955 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000956 return Op;
957 }
958
Chris Lattner3a697562010-10-28 17:20:03 +0000959 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
960 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000961 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000962 Op->StartLoc = S;
963 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000964 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000965 }
966
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000967 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
968 bool OffsetIsReg, const MCExpr *Offset,
969 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000970 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000971 const MCExpr *ShiftAmount, bool Preindexed,
972 bool Postindexed, bool Negative, bool Writeback,
973 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000974 assert((OffsetRegNum == -1 || OffsetIsReg) &&
975 "OffsetRegNum must imply OffsetIsReg!");
976 assert((!OffsetRegShifted || OffsetIsReg) &&
977 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000978 assert((Offset || OffsetIsReg) &&
979 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000980 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
981 "Cannot have shift amount without shifted register offset!");
982 assert((!Offset || !OffsetIsReg) &&
983 "Cannot have expression offset and register offset!");
984
Chris Lattner3a697562010-10-28 17:20:03 +0000985 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000986 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000987 Op->Mem.BaseRegNum = BaseRegNum;
988 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000989 if (OffsetIsReg)
990 Op->Mem.Offset.RegNum = OffsetRegNum;
991 else
992 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000993 Op->Mem.OffsetRegShifted = OffsetRegShifted;
994 Op->Mem.ShiftType = ShiftType;
995 Op->Mem.ShiftAmount = ShiftAmount;
996 Op->Mem.Preindexed = Preindexed;
997 Op->Mem.Postindexed = Postindexed;
998 Op->Mem.Negative = Negative;
999 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +00001000
Sean Callanan76264762010-04-02 22:27:05 +00001001 Op->StartLoc = S;
1002 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001003 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001004 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001005
1006 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
1007 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
1008 Op->MBOpt.Val = Opt;
1009 Op->StartLoc = S;
1010 Op->EndLoc = S;
1011 return Op;
1012 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001013
1014 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1015 ARMOperand *Op = new ARMOperand(ProcIFlags);
1016 Op->IFlags.Val = IFlags;
1017 Op->StartLoc = S;
1018 Op->EndLoc = S;
1019 return Op;
1020 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001021
1022 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1023 ARMOperand *Op = new ARMOperand(MSRMask);
1024 Op->MMask.Val = MMask;
1025 Op->StartLoc = S;
1026 Op->EndLoc = S;
1027 return Op;
1028 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001029};
1030
1031} // end anonymous namespace.
1032
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001033void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001034 switch (Kind) {
1035 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +00001036 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001037 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +00001038 case CCOut:
1039 OS << "<ccout " << getReg() << ">";
1040 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001041 case CoprocNum:
1042 OS << "<coprocessor number: " << getCoproc() << ">";
1043 break;
1044 case CoprocReg:
1045 OS << "<coprocessor register: " << getCoproc() << ">";
1046 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001047 case MSRMask:
1048 OS << "<mask: " << getMSRMask() << ">";
1049 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001050 case Immediate:
1051 getImm()->print(OS);
1052 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001053 case MemBarrierOpt:
1054 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1055 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001056 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001057 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001058 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
1059 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001060 if (getMemOffsetIsReg()) {
1061 OS << " offset:<register " << getMemOffsetRegNum();
1062 if (getMemOffsetRegShifted()) {
1063 OS << " offset-shift-type:" << getMemShiftType();
1064 OS << " offset-shift-amount:" << *getMemShiftAmount();
1065 }
1066 } else {
1067 OS << " offset:" << *getMemOffset();
1068 }
1069 if (getMemOffsetIsReg())
1070 OS << " (offset-is-reg)";
1071 if (getMemPreindexed())
1072 OS << " (pre-indexed)";
1073 if (getMemPostindexed())
1074 OS << " (post-indexed)";
1075 if (getMemNegative())
1076 OS << " (negative)";
1077 if (getMemWriteback())
1078 OS << " (writeback)";
1079 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001080 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001081 case ProcIFlags: {
1082 OS << "<ARM_PROC::";
1083 unsigned IFlags = getProcIFlags();
1084 for (int i=2; i >= 0; --i)
1085 if (IFlags & (1 << i))
1086 OS << ARM_PROC::IFlagsToString(1 << i);
1087 OS << ">";
1088 break;
1089 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001090 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +00001091 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001092 break;
Owen Anderson00828302011-03-18 22:50:18 +00001093 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +00001094 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
1095 break;
1096 case ShiftedRegister:
Owen Anderson92a20222011-07-21 18:54:16 +00001097 OS << "<so_reg_reg "
Jim Grosbache8606dc2011-07-13 17:50:29 +00001098 << ShiftedReg.SrcReg
1099 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
1100 << ", " << ShiftedReg.ShiftReg << ", "
1101 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
1102 << ">";
Owen Anderson00828302011-03-18 22:50:18 +00001103 break;
Owen Anderson92a20222011-07-21 18:54:16 +00001104 case ShiftedImmediate:
1105 OS << "<so_reg_imm "
1106 << ShiftedImm.SrcReg
1107 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedImm.ShiftImm))
1108 << ", " << ARM_AM::getSORegOffset(ShiftedImm.ShiftImm)
1109 << ">";
1110 break;
Bill Wendling0f630752010-11-17 04:32:08 +00001111 case RegisterList:
1112 case DPRRegisterList:
1113 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00001114 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001115
Bill Wendling5fa22a12010-11-09 23:28:44 +00001116 const SmallVectorImpl<unsigned> &RegList = getRegList();
1117 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001118 I = RegList.begin(), E = RegList.end(); I != E; ) {
1119 OS << *I;
1120 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001121 }
1122
1123 OS << ">";
1124 break;
1125 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001126 case Token:
1127 OS << "'" << getToken() << "'";
1128 break;
1129 }
1130}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001131
1132/// @name Auto-generated Match Functions
1133/// {
1134
1135static unsigned MatchRegisterName(StringRef Name);
1136
1137/// }
1138
Bob Wilson69df7232011-02-03 21:46:10 +00001139bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1140 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +00001141 RegNo = TryParseRegister();
1142
1143 return (RegNo == (unsigned)-1);
1144}
1145
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001146/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00001147/// and if it is a register name the token is eaten and the register number is
1148/// returned. Otherwise return -1.
1149///
1150int ARMAsmParser::TryParseRegister() {
1151 const AsmToken &Tok = Parser.getTok();
1152 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001153
Chris Lattnere5658fa2010-10-30 04:09:10 +00001154 // FIXME: Validate register for the current architecture; we have to do
1155 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001156 std::string upperCase = Tok.getString().str();
1157 std::string lowerCase = LowercaseString(upperCase);
1158 unsigned RegNum = MatchRegisterName(lowerCase);
1159 if (!RegNum) {
1160 RegNum = StringSwitch<unsigned>(lowerCase)
1161 .Case("r13", ARM::SP)
1162 .Case("r14", ARM::LR)
1163 .Case("r15", ARM::PC)
1164 .Case("ip", ARM::R12)
1165 .Default(0);
1166 }
1167 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001168
Chris Lattnere5658fa2010-10-30 04:09:10 +00001169 Parser.Lex(); // Eat identifier token.
1170 return RegNum;
1171}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001172
Jim Grosbach19906722011-07-13 18:49:30 +00001173// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1174// If a recoverable error occurs, return 1. If an irrecoverable error
1175// occurs, return -1. An irrecoverable error is one where tokens have been
1176// consumed in the process of trying to parse the shifter (i.e., when it is
1177// indeed a shifter operand, but malformed).
1178int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001179 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1180 SMLoc S = Parser.getTok().getLoc();
1181 const AsmToken &Tok = Parser.getTok();
1182 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1183
1184 std::string upperCase = Tok.getString().str();
1185 std::string lowerCase = LowercaseString(upperCase);
1186 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1187 .Case("lsl", ARM_AM::lsl)
1188 .Case("lsr", ARM_AM::lsr)
1189 .Case("asr", ARM_AM::asr)
1190 .Case("ror", ARM_AM::ror)
1191 .Case("rrx", ARM_AM::rrx)
1192 .Default(ARM_AM::no_shift);
1193
1194 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001195 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001196
Jim Grosbache8606dc2011-07-13 17:50:29 +00001197 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001198
Jim Grosbache8606dc2011-07-13 17:50:29 +00001199 // The source register for the shift has already been added to the
1200 // operand list, so we need to pop it off and combine it into the shifted
1201 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00001202 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00001203 if (!PrevOp->isReg())
1204 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1205 int SrcReg = PrevOp->getReg();
1206 int64_t Imm = 0;
1207 int ShiftReg = 0;
1208 if (ShiftTy == ARM_AM::rrx) {
1209 // RRX Doesn't have an explicit shift amount. The encoder expects
1210 // the shift register to be the same as the source register. Seems odd,
1211 // but OK.
1212 ShiftReg = SrcReg;
1213 } else {
1214 // Figure out if this is shifted by a constant or a register (for non-RRX).
1215 if (Parser.getTok().is(AsmToken::Hash)) {
1216 Parser.Lex(); // Eat hash.
1217 SMLoc ImmLoc = Parser.getTok().getLoc();
1218 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001219 if (getParser().ParseExpression(ShiftExpr)) {
1220 Error(ImmLoc, "invalid immediate shift value");
1221 return -1;
1222 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001223 // The expression must be evaluatable as an immediate.
1224 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001225 if (!CE) {
1226 Error(ImmLoc, "invalid immediate shift value");
1227 return -1;
1228 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001229 // Range check the immediate.
1230 // lsl, ror: 0 <= imm <= 31
1231 // lsr, asr: 0 <= imm <= 32
1232 Imm = CE->getValue();
1233 if (Imm < 0 ||
1234 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1235 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001236 Error(ImmLoc, "immediate shift value out of range");
1237 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001238 }
1239 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1240 ShiftReg = TryParseRegister();
1241 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001242 if (ShiftReg == -1) {
1243 Error (L, "expected immediate or register in shift operand");
1244 return -1;
1245 }
1246 } else {
1247 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001248 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001249 return -1;
1250 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001251 }
1252
Owen Anderson92a20222011-07-21 18:54:16 +00001253 if (ShiftReg && ShiftTy != ARM_AM::rrx)
1254 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
Jim Grosbache8606dc2011-07-13 17:50:29 +00001255 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001256 S, Parser.getTok().getLoc()));
Owen Anderson92a20222011-07-21 18:54:16 +00001257 else
1258 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
1259 S, Parser.getTok().getLoc()));
Owen Anderson00828302011-03-18 22:50:18 +00001260
Jim Grosbach19906722011-07-13 18:49:30 +00001261 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001262}
1263
1264
Bill Wendling50d0f582010-11-18 23:43:05 +00001265/// Try to parse a register name. The token must be an Identifier when called.
1266/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1267/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001268///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001269/// TODO this is likely to change to allow different register types and or to
1270/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001271bool ARMAsmParser::
1272TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001273 SMLoc S = Parser.getTok().getLoc();
1274 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001275 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001276 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001277
Bill Wendling50d0f582010-11-18 23:43:05 +00001278 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001279
Chris Lattnere5658fa2010-10-30 04:09:10 +00001280 const AsmToken &ExclaimTok = Parser.getTok();
1281 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001282 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1283 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001284 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001285 }
1286
Bill Wendling50d0f582010-11-18 23:43:05 +00001287 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001288}
1289
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001290/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1291/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1292/// "c5", ...
1293static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001294 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1295 // but efficient.
1296 switch (Name.size()) {
1297 default: break;
1298 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001299 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001300 return -1;
1301 switch (Name[1]) {
1302 default: return -1;
1303 case '0': return 0;
1304 case '1': return 1;
1305 case '2': return 2;
1306 case '3': return 3;
1307 case '4': return 4;
1308 case '5': return 5;
1309 case '6': return 6;
1310 case '7': return 7;
1311 case '8': return 8;
1312 case '9': return 9;
1313 }
1314 break;
1315 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001316 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001317 return -1;
1318 switch (Name[2]) {
1319 default: return -1;
1320 case '0': return 10;
1321 case '1': return 11;
1322 case '2': return 12;
1323 case '3': return 13;
1324 case '4': return 14;
1325 case '5': return 15;
1326 }
1327 break;
1328 }
1329
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001330 return -1;
1331}
1332
Jim Grosbachf922c472011-02-12 01:34:40 +00001333/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001334/// token must be an Identifier when called, and if it is a coprocessor
1335/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001336ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1337tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001338 SMLoc S = Parser.getTok().getLoc();
1339 const AsmToken &Tok = Parser.getTok();
1340 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1341
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001342 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001343 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001344 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001345
1346 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001347 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001348 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001349}
1350
Jim Grosbachf922c472011-02-12 01:34:40 +00001351/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001352/// token must be an Identifier when called, and if it is a coprocessor
1353/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001354ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1355tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001356 SMLoc S = Parser.getTok().getLoc();
1357 const AsmToken &Tok = Parser.getTok();
1358 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1359
1360 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1361 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001362 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001363
1364 Parser.Lex(); // Eat identifier token.
1365 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001366 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001367}
1368
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001369/// Parse a register list, return it if successful else return null. The first
1370/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001371bool ARMAsmParser::
1372ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001373 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001374 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001375 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001376
Bill Wendling7729e062010-11-09 22:44:22 +00001377 // Read the rest of the registers in the list.
1378 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001379 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001380
Bill Wendling7729e062010-11-09 22:44:22 +00001381 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001382 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001383 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001384
Sean Callanan18b83232010-01-19 21:44:56 +00001385 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001386 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001387 if (RegTok.isNot(AsmToken::Identifier)) {
1388 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001389 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001390 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001391
Bill Wendling1d6a2652010-11-06 10:40:24 +00001392 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001393 if (RegNum == -1) {
1394 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001395 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001396 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001397
Bill Wendlinge7176102010-11-06 22:36:58 +00001398 if (IsRange) {
1399 int Reg = PrevRegNum;
1400 do {
1401 ++Reg;
1402 Registers.push_back(std::make_pair(Reg, RegLoc));
1403 } while (Reg != RegNum);
1404 } else {
1405 Registers.push_back(std::make_pair(RegNum, RegLoc));
1406 }
1407
1408 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001409 } while (Parser.getTok().is(AsmToken::Comma) ||
1410 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001411
1412 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001413 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001414 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1415 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001416 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001417 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001418
Bill Wendlinge7176102010-11-06 22:36:58 +00001419 SMLoc E = RCurlyTok.getLoc();
1420 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001421
Bill Wendlinge7176102010-11-06 22:36:58 +00001422 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001423 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001424 RI = Registers.begin(), RE = Registers.end();
1425
Bill Wendling7caebff2011-01-12 21:20:59 +00001426 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001427 bool EmittedWarning = false;
1428
Bill Wendling7caebff2011-01-12 21:20:59 +00001429 DenseMap<unsigned, bool> RegMap;
1430 RegMap[HighRegNum] = true;
1431
Bill Wendlinge7176102010-11-06 22:36:58 +00001432 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001433 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001434 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001435
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001436 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001437 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001438 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001439 }
1440
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001441 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001442 Warning(RegInfo.second,
1443 "register not in ascending order in register list");
1444
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001445 RegMap[Reg] = true;
1446 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001447 }
1448
Bill Wendling50d0f582010-11-18 23:43:05 +00001449 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1450 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001451}
1452
Jim Grosbachf922c472011-02-12 01:34:40 +00001453/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1454ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1455tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001456 SMLoc S = Parser.getTok().getLoc();
1457 const AsmToken &Tok = Parser.getTok();
1458 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1459 StringRef OptStr = Tok.getString();
1460
1461 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1462 .Case("sy", ARM_MB::SY)
1463 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001464 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001465 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001466 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001467 .Case("ishst", ARM_MB::ISHST)
1468 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001469 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001470 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001471 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001472 .Case("osh", ARM_MB::OSH)
1473 .Case("oshst", ARM_MB::OSHST)
1474 .Default(~0U);
1475
1476 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001477 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001478
1479 Parser.Lex(); // Eat identifier token.
1480 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001481 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001482}
1483
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001484/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001485ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1486tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1487 SMLoc S = Parser.getTok().getLoc();
1488 const AsmToken &Tok = Parser.getTok();
1489 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1490 StringRef IFlagsStr = Tok.getString();
1491
1492 unsigned IFlags = 0;
1493 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1494 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1495 .Case("a", ARM_PROC::A)
1496 .Case("i", ARM_PROC::I)
1497 .Case("f", ARM_PROC::F)
1498 .Default(~0U);
1499
1500 // If some specific iflag is already set, it means that some letter is
1501 // present more than once, this is not acceptable.
1502 if (Flag == ~0U || (IFlags & Flag))
1503 return MatchOperand_NoMatch;
1504
1505 IFlags |= Flag;
1506 }
1507
1508 Parser.Lex(); // Eat identifier token.
1509 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1510 return MatchOperand_Success;
1511}
1512
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001513/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1514ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1515tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1516 SMLoc S = Parser.getTok().getLoc();
1517 const AsmToken &Tok = Parser.getTok();
1518 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1519 StringRef Mask = Tok.getString();
1520
1521 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1522 size_t Start = 0, Next = Mask.find('_');
1523 StringRef Flags = "";
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00001524 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001525 if (Next != StringRef::npos)
1526 Flags = Mask.slice(Next+1, Mask.size());
1527
1528 // FlagsVal contains the complete mask:
1529 // 3-0: Mask
1530 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1531 unsigned FlagsVal = 0;
1532
1533 if (SpecReg == "apsr") {
1534 FlagsVal = StringSwitch<unsigned>(Flags)
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00001535 .Case("nzcvq", 0x8) // same as CPSR_f
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001536 .Case("g", 0x4) // same as CPSR_s
1537 .Case("nzcvqg", 0xc) // same as CPSR_fs
1538 .Default(~0U);
1539
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001540 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001541 if (!Flags.empty())
1542 return MatchOperand_NoMatch;
1543 else
1544 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001545 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001546 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001547 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1548 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001549 for (int i = 0, e = Flags.size(); i != e; ++i) {
1550 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1551 .Case("c", 1)
1552 .Case("x", 2)
1553 .Case("s", 4)
1554 .Case("f", 8)
1555 .Default(~0U);
1556
1557 // If some specific flag is already set, it means that some letter is
1558 // present more than once, this is not acceptable.
1559 if (FlagsVal == ~0U || (FlagsVal & Flag))
1560 return MatchOperand_NoMatch;
1561 FlagsVal |= Flag;
1562 }
1563 } else // No match for special register.
1564 return MatchOperand_NoMatch;
1565
1566 // Special register without flags are equivalent to "fc" flags.
1567 if (!FlagsVal)
1568 FlagsVal = 0x9;
1569
1570 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1571 if (SpecReg == "spsr")
1572 FlagsVal |= 16;
1573
1574 Parser.Lex(); // Eat identifier token.
1575 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1576 return MatchOperand_Success;
1577}
1578
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001579/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1580ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1581tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001582 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001583
1584 if (ParseMemory(Operands, ARMII::AddrMode2))
1585 return MatchOperand_NoMatch;
1586
1587 return MatchOperand_Success;
1588}
1589
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001590/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1591ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1592tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1593 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1594
1595 if (ParseMemory(Operands, ARMII::AddrMode3))
1596 return MatchOperand_NoMatch;
1597
1598 return MatchOperand_Success;
1599}
1600
Jim Grosbachf6c05252011-07-21 17:23:04 +00001601ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1602parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
1603 int Low, int High) {
1604 const AsmToken &Tok = Parser.getTok();
1605 if (Tok.isNot(AsmToken::Identifier)) {
1606 Error(Parser.getTok().getLoc(), Op + " operand expected.");
1607 return MatchOperand_ParseFail;
1608 }
1609 StringRef ShiftName = Tok.getString();
1610 std::string LowerOp = LowercaseString(Op);
1611 std::string UpperOp = UppercaseString(Op);
1612 if (ShiftName != LowerOp && ShiftName != UpperOp) {
1613 Error(Parser.getTok().getLoc(), Op + " operand expected.");
1614 return MatchOperand_ParseFail;
1615 }
1616 Parser.Lex(); // Eat shift type token.
1617
1618 // There must be a '#' and a shift amount.
1619 if (Parser.getTok().isNot(AsmToken::Hash)) {
1620 Error(Parser.getTok().getLoc(), "'#' expected");
1621 return MatchOperand_ParseFail;
1622 }
1623 Parser.Lex(); // Eat hash token.
1624
1625 const MCExpr *ShiftAmount;
1626 SMLoc Loc = Parser.getTok().getLoc();
1627 if (getParser().ParseExpression(ShiftAmount)) {
1628 Error(Loc, "illegal expression");
1629 return MatchOperand_ParseFail;
1630 }
1631 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
1632 if (!CE) {
1633 Error(Loc, "constant expression expected");
1634 return MatchOperand_ParseFail;
1635 }
1636 int Val = CE->getValue();
1637 if (Val < Low || Val > High) {
1638 Error(Loc, "immediate value out of range");
1639 return MatchOperand_ParseFail;
1640 }
1641
1642 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
1643
1644 return MatchOperand_Success;
1645}
1646
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001647/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1648/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1649/// when they refer multiple MIOperands inside a single one.
1650bool ARMAsmParser::
1651CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1652 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1653 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1654
1655 // Create a writeback register dummy placeholder.
1656 Inst.addOperand(MCOperand::CreateImm(0));
1657
1658 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1659 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1660 return true;
1661}
1662
1663/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1664/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1665/// when they refer multiple MIOperands inside a single one.
1666bool ARMAsmParser::
1667CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1668 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1669 // Create a writeback register dummy placeholder.
1670 Inst.addOperand(MCOperand::CreateImm(0));
1671 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1672 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1673 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1674 return true;
1675}
1676
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001677/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1678/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1679/// when they refer multiple MIOperands inside a single one.
1680bool ARMAsmParser::
1681CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1682 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1683 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1684
1685 // Create a writeback register dummy placeholder.
1686 Inst.addOperand(MCOperand::CreateImm(0));
1687
1688 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1689 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1690 return true;
1691}
1692
1693/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1694/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1695/// when they refer multiple MIOperands inside a single one.
1696bool ARMAsmParser::
1697CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1698 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1699 // Create a writeback register dummy placeholder.
1700 Inst.addOperand(MCOperand::CreateImm(0));
1701 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1702 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1703 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1704 return true;
1705}
1706
Bill Wendlinge7176102010-11-06 22:36:58 +00001707/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001708/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001709///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001710/// TODO Only preindexing and postindexing addressing are started, unindexed
1711/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001712bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001713ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1714 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001715 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001716 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001717 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001718 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001719 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001720
Sean Callanan18b83232010-01-19 21:44:56 +00001721 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001722 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1723 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001724 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001725 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001726 int BaseRegNum = TryParseRegister();
1727 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001728 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001729 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001730 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001731
Daniel Dunbar05710932011-01-18 05:34:17 +00001732 // The next token must either be a comma or a closing bracket.
1733 const AsmToken &Tok = Parser.getTok();
1734 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1735 return true;
1736
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001737 bool Preindexed = false;
1738 bool Postindexed = false;
1739 bool OffsetIsReg = false;
1740 bool Negative = false;
1741 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001742 ARMOperand *WBOp = 0;
1743 int OffsetRegNum = -1;
1744 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001745 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001746 const MCExpr *ShiftAmount = 0;
1747 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001748
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001749 // First look for preindexed address forms, that is after the "[Rn" we now
1750 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001751 if (Tok.is(AsmToken::Comma)) {
1752 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001753 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001754
Chris Lattner550276e2010-10-28 20:52:15 +00001755 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1756 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001757 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001758 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001759 if (RBracTok.isNot(AsmToken::RBrac)) {
1760 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001761 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001762 }
Sean Callanan76264762010-04-02 22:27:05 +00001763 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001764 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001765
Sean Callanan18b83232010-01-19 21:44:56 +00001766 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001767 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001768 // None of addrmode3 instruction uses "!"
1769 if (AddrMode == ARMII::AddrMode3)
1770 return true;
1771
Bill Wendling50d0f582010-11-18 23:43:05 +00001772 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1773 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001774 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001775 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001776 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1777 if (AddrMode == ARMII::AddrMode2)
1778 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001779 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001780 } else {
1781 // The "[Rn" we have so far was not followed by a comma.
1782
Jim Grosbach80eb2332010-10-29 17:41:25 +00001783 // If there's anything other than the right brace, this is a post indexing
1784 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001785 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001786 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001787
Sean Callanan18b83232010-01-19 21:44:56 +00001788 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001789
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001790 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001791 Postindexed = true;
1792 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001793
Chris Lattner550276e2010-10-28 20:52:15 +00001794 if (NextTok.isNot(AsmToken::Comma)) {
1795 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001796 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001797 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001798
Sean Callananb9a25b72010-01-19 20:27:46 +00001799 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001800
Chris Lattner550276e2010-10-28 20:52:15 +00001801 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001802 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001803 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001804 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001805 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001806 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001807
1808 // Force Offset to exist if used.
1809 if (!OffsetIsReg) {
1810 if (!Offset)
1811 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001812 } else {
1813 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1814 Error(E, "shift amount not supported");
1815 return true;
1816 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001817 }
1818
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001819 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1820 Offset, OffsetRegNum, OffsetRegShifted,
1821 ShiftType, ShiftAmount, Preindexed,
1822 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001823 if (WBOp)
1824 Operands.push_back(WBOp);
1825
1826 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001827}
1828
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001829/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1830/// we will parse the following (were +/- means that a plus or minus is
1831/// optional):
1832/// +/-Rm
1833/// +/-Rm, shift
1834/// #offset
1835/// we return false on success or an error otherwise.
1836bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001837 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001838 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001839 const MCExpr *&ShiftAmount,
1840 const MCExpr *&Offset,
1841 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001842 int &OffsetRegNum,
1843 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001844 Negative = false;
1845 OffsetRegShifted = false;
1846 OffsetIsReg = false;
1847 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001848 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001849 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001850 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001851 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001852 else if (NextTok.is(AsmToken::Minus)) {
1853 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001854 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001855 }
1856 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001857 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001858 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001859 SMLoc CurLoc = OffsetRegTok.getLoc();
1860 OffsetRegNum = TryParseRegister();
1861 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001862 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001863 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001864 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001865 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001866
Bill Wendling12f40e92010-11-06 10:51:53 +00001867 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001868 if (OffsetRegNum != -1) {
1869 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001870 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001871 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001872 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001873
Sean Callanan18b83232010-01-19 21:44:56 +00001874 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001875 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001876 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001877 OffsetRegShifted = true;
1878 }
1879 }
1880 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1881 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001882 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001883 if (HashTok.isNot(AsmToken::Hash))
1884 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001885
Sean Callananb9a25b72010-01-19 20:27:46 +00001886 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001887
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001888 if (getParser().ParseExpression(Offset))
1889 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001890 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001891 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001892 return false;
1893}
1894
1895/// ParseShift as one of these two:
1896/// ( lsl | lsr | asr | ror ) , # shift_amount
1897/// rrx
1898/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001899bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1900 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001901 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001902 if (Tok.isNot(AsmToken::Identifier))
1903 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001904 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001905 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001906 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001907 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001908 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001909 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001910 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001911 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001912 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001913 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001914 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001915 else
1916 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001917 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001918
1919 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001920 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001921 return false;
1922
1923 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001924 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001925 if (HashTok.isNot(AsmToken::Hash))
1926 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001927 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001928
1929 if (getParser().ParseExpression(ShiftAmount))
1930 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001931
1932 return false;
1933}
1934
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001935/// Parse a arm instruction operand. For now this parses the operand regardless
1936/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001937bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001938 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001939 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001940
1941 // Check if the current operand has a custom associated parser, if so, try to
1942 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001943 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1944 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001945 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001946 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1947 // there was a match, but an error occurred, in which case, just return that
1948 // the operand parsing failed.
1949 if (ResTy == MatchOperand_ParseFail)
1950 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001951
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001952 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001953 default:
1954 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001955 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001956 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001957 if (!TryParseRegisterWithWriteBack(Operands))
1958 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001959 int Res = TryParseShiftRegister(Operands);
1960 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001961 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001962 else if (Res == -1) // irrecoverable error
1963 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001964
1965 // Fall though for the Identifier case that is not a register or a
1966 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00001967 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00001968 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1969 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001970 // This was not a register so parse other operands that start with an
1971 // identifier (like labels) as expressions and create them as immediates.
1972 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001973 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001974 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001975 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001976 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001977 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1978 return false;
1979 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001980 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001981 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001982 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001983 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001984 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001985 // #42 -> immediate.
1986 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001987 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001988 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001989 const MCExpr *ImmVal;
1990 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001991 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001992 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001993 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1994 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001995 case AsmToken::Colon: {
1996 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001997 // FIXME: Check it's an expression prefix,
1998 // e.g. (FOO - :lower16:BAR) isn't legal.
1999 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002000 if (ParsePrefix(RefKind))
2001 return true;
2002
Evan Cheng75972122011-01-13 07:58:56 +00002003 const MCExpr *SubExprVal;
2004 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00002005 return true;
2006
Evan Cheng75972122011-01-13 07:58:56 +00002007 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
2008 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00002009 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00002010 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00002011 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002012 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00002013 }
2014}
2015
Evan Cheng75972122011-01-13 07:58:56 +00002016// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
2017// :lower16: and :upper16:.
2018bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
2019 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002020
2021 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00002022 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00002023 Parser.Lex(); // Eat ':'
2024
2025 if (getLexer().isNot(AsmToken::Identifier)) {
2026 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
2027 return true;
2028 }
2029
2030 StringRef IDVal = Parser.getTok().getIdentifier();
2031 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00002032 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002033 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00002034 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002035 } else {
2036 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
2037 return true;
2038 }
2039 Parser.Lex();
2040
2041 if (getLexer().isNot(AsmToken::Colon)) {
2042 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
2043 return true;
2044 }
2045 Parser.Lex(); // Eat the last ':'
2046 return false;
2047}
2048
2049const MCExpr *
2050ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
2051 MCSymbolRefExpr::VariantKind Variant) {
2052 // Recurse over the given expression, rebuilding it to apply the given variant
2053 // to the leftmost symbol.
2054 if (Variant == MCSymbolRefExpr::VK_None)
2055 return E;
2056
2057 switch (E->getKind()) {
2058 case MCExpr::Target:
2059 llvm_unreachable("Can't handle target expr yet");
2060 case MCExpr::Constant:
2061 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
2062
2063 case MCExpr::SymbolRef: {
2064 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
2065
2066 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
2067 return 0;
2068
2069 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
2070 }
2071
2072 case MCExpr::Unary:
2073 llvm_unreachable("Can't handle unary expressions yet");
2074
2075 case MCExpr::Binary: {
2076 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
2077 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
2078 const MCExpr *RHS = BE->getRHS();
2079 if (!LHS)
2080 return 0;
2081
2082 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
2083 }
2084 }
2085
2086 assert(0 && "Invalid expression kind!");
2087 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002088}
2089
Daniel Dunbar352e1482011-01-11 15:59:50 +00002090/// \brief Given a mnemonic, split out possible predication code and carry
2091/// setting letters to form a canonical mnemonic and flags.
2092//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002093// FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00002094StringRef ARMAsmParser::SplitMnemonic(StringRef Mnemonic,
2095 unsigned &PredicationCode,
2096 bool &CarrySetting,
2097 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00002098 PredicationCode = ARMCC::AL;
2099 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002100 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002101
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002102 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00002103 //
2104 // FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00002105 if ((Mnemonic == "movs" && isThumb()) ||
2106 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
2107 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
2108 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
2109 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
2110 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
2111 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
2112 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
Daniel Dunbar352e1482011-01-11 15:59:50 +00002113 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00002114
Jim Grosbach3f00e312011-07-11 17:09:57 +00002115 // First, split out any predication code. Ignore mnemonics we know aren't
2116 // predicated but do have a carry-set and so weren't caught above.
Jim Grosbachab40f4b2011-07-20 18:20:31 +00002117 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
2118 Mnemonic != "muls") {
Jim Grosbach3f00e312011-07-11 17:09:57 +00002119 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
2120 .Case("eq", ARMCC::EQ)
2121 .Case("ne", ARMCC::NE)
2122 .Case("hs", ARMCC::HS)
2123 .Case("cs", ARMCC::HS)
2124 .Case("lo", ARMCC::LO)
2125 .Case("cc", ARMCC::LO)
2126 .Case("mi", ARMCC::MI)
2127 .Case("pl", ARMCC::PL)
2128 .Case("vs", ARMCC::VS)
2129 .Case("vc", ARMCC::VC)
2130 .Case("hi", ARMCC::HI)
2131 .Case("ls", ARMCC::LS)
2132 .Case("ge", ARMCC::GE)
2133 .Case("lt", ARMCC::LT)
2134 .Case("gt", ARMCC::GT)
2135 .Case("le", ARMCC::LE)
2136 .Case("al", ARMCC::AL)
2137 .Default(~0U);
2138 if (CC != ~0U) {
2139 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
2140 PredicationCode = CC;
2141 }
Bill Wendling52925b62010-10-29 23:50:21 +00002142 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002143
Daniel Dunbar352e1482011-01-11 15:59:50 +00002144 // Next, determine if we have a carry setting bit. We explicitly ignore all
2145 // the instructions we know end in 's'.
2146 if (Mnemonic.endswith("s") &&
2147 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002148 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
2149 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
2150 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
2151 Mnemonic == "vrsqrts" || (Mnemonic == "movs" && isThumb()))) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00002152 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
2153 CarrySetting = true;
2154 }
2155
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002156 // The "cps" instruction can have a interrupt mode operand which is glued into
2157 // the mnemonic. Check if this is the case, split it and parse the imod op
2158 if (Mnemonic.startswith("cps")) {
2159 // Split out any imod code.
2160 unsigned IMod =
2161 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
2162 .Case("ie", ARM_PROC::IE)
2163 .Case("id", ARM_PROC::ID)
2164 .Default(~0U);
2165 if (IMod != ~0U) {
2166 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2167 ProcessorIMod = IMod;
2168 }
2169 }
2170
Daniel Dunbar352e1482011-01-11 15:59:50 +00002171 return Mnemonic;
2172}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002173
2174/// \brief Given a canonical mnemonic, determine if the instruction ever allows
2175/// inclusion of carry set or predication code operands.
2176//
2177// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00002178void ARMAsmParser::
2179GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2180 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002181 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2182 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2183 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2184 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002185 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002186 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2187 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002188 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00002189 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002190 CanAcceptCarrySet = true;
2191 } else {
2192 CanAcceptCarrySet = false;
2193 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002194
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002195 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2196 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2197 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2198 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002199 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "clrex" ||
2200 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumb())) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002201 CanAcceptPredicationCode = false;
2202 } else {
2203 CanAcceptPredicationCode = true;
2204 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002205
Evan Chengebdeeab2011-07-08 01:53:10 +00002206 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002207 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002208 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002209 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002210}
2211
2212/// Parse an arm instruction mnemonic followed by its operands.
2213bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2214 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2215 // Create the leading tokens for the mnemonic, split by '.' characters.
2216 size_t Start = 0, Next = Name.find('.');
Jim Grosbachffa32252011-07-19 19:13:28 +00002217 StringRef Mnemonic = Name.slice(Start, Next);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002218
Daniel Dunbar352e1482011-01-11 15:59:50 +00002219 // Split out the predication code and carry setting flag from the mnemonic.
2220 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002221 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002222 bool CarrySetting;
Jim Grosbachffa32252011-07-19 19:13:28 +00002223 Mnemonic = SplitMnemonic(Mnemonic, PredicationCode, CarrySetting,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002224 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002225
Jim Grosbachffa32252011-07-19 19:13:28 +00002226 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
2227
2228 // FIXME: This is all a pretty gross hack. We should automatically handle
2229 // optional operands like this via tblgen.
Bill Wendling9717fa92010-11-21 10:56:05 +00002230
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002231 // Next, add the CCOut and ConditionCode operands, if needed.
2232 //
2233 // For mnemonics which can ever incorporate a carry setting bit or predication
2234 // code, our matching model involves us always generating CCOut and
2235 // ConditionCode operands to match the mnemonic "as written" and then we let
2236 // the matcher deal with finding the right instruction or generating an
2237 // appropriate error.
2238 bool CanAcceptCarrySet, CanAcceptPredicationCode;
Jim Grosbachffa32252011-07-19 19:13:28 +00002239 GetMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002240
Jim Grosbach33c16a22011-07-14 22:04:21 +00002241 // If we had a carry-set on an instruction that can't do that, issue an
2242 // error.
2243 if (!CanAcceptCarrySet && CarrySetting) {
2244 Parser.EatToEndOfStatement();
Jim Grosbachffa32252011-07-19 19:13:28 +00002245 return Error(NameLoc, "instruction '" + Mnemonic +
Jim Grosbach33c16a22011-07-14 22:04:21 +00002246 "' can not set flags, but 's' suffix specified");
2247 }
2248
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002249 // Add the carry setting operand, if necessary.
2250 //
2251 // FIXME: It would be awesome if we could somehow invent a location such that
2252 // match errors on this operand would print a nice diagnostic about how the
2253 // 's' character in the mnemonic resulted in a CCOut operand.
Jim Grosbach33c16a22011-07-14 22:04:21 +00002254 if (CanAcceptCarrySet)
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002255 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2256 NameLoc));
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002257
2258 // Add the predication code operand, if necessary.
2259 if (CanAcceptPredicationCode) {
2260 Operands.push_back(ARMOperand::CreateCondCode(
2261 ARMCC::CondCodes(PredicationCode), NameLoc));
2262 } else {
2263 // This mnemonic can't ever accept a predication code, but the user wrote
2264 // one (or misspelled another mnemonic).
2265
2266 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002267 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002268
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002269 // Add the processor imod operand, if necessary.
2270 if (ProcessorIMod) {
2271 Operands.push_back(ARMOperand::CreateImm(
2272 MCConstantExpr::Create(ProcessorIMod, getContext()),
2273 NameLoc, NameLoc));
2274 } else {
2275 // This mnemonic can't ever accept a imod, but the user wrote
2276 // one (or misspelled another mnemonic).
2277
2278 // FIXME: Issue a nice error.
2279 }
2280
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002281 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002282 while (Next != StringRef::npos) {
2283 Start = Next;
2284 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002285 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002286
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002287 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002288 }
2289
2290 // Read the remaining operands.
2291 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002292 // Read the first operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002293 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002294 Parser.EatToEndOfStatement();
2295 return true;
2296 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002297
2298 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002299 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002300
2301 // Parse and remember the operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002302 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002303 Parser.EatToEndOfStatement();
2304 return true;
2305 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002306 }
2307 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002308
Chris Lattnercbf8a982010-09-11 16:18:25 +00002309 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2310 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002311 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002312 }
Bill Wendling146018f2010-11-06 21:42:12 +00002313
Chris Lattner34e53142010-09-08 05:10:46 +00002314 Parser.Lex(); // Consume the EndOfStatement
Jim Grosbachffa32252011-07-19 19:13:28 +00002315
2316
2317 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
2318 // another does not. Specifically, the MOVW instruction does not. So we
2319 // special case it here and remove the defaulted (non-setting) cc_out
2320 // operand if that's the instruction we're trying to match.
2321 //
2322 // We do this post-processing of the explicit operands rather than just
2323 // conditionally adding the cc_out in the first place because we need
2324 // to check the type of the parsed immediate operand.
2325 if (Mnemonic == "mov" && Operands.size() > 4 &&
2326 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
Jim Grosbach731f2092011-07-19 19:45:44 +00002327 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
2328 static_cast<ARMOperand*>(Operands[1])->getReg() == 0) {
Jim Grosbachffa32252011-07-19 19:13:28 +00002329 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
2330 Operands.erase(Operands.begin() + 1);
2331 delete Op;
2332 }
2333
Chris Lattner98986712010-01-14 22:21:20 +00002334 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002335}
2336
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002337bool ARMAsmParser::
2338MatchAndEmitInstruction(SMLoc IDLoc,
2339 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2340 MCStreamer &Out) {
2341 MCInst Inst;
2342 unsigned ErrorInfo;
Jim Grosbach5a187002011-07-19 18:32:48 +00002343 MatchResultTy MatchResult;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002344 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002345 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002346 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002347 Out.EmitInstruction(Inst);
2348 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002349 case Match_MissingFeature:
2350 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2351 return true;
2352 case Match_InvalidOperand: {
2353 SMLoc ErrorLoc = IDLoc;
2354 if (ErrorInfo != ~0U) {
2355 if (ErrorInfo >= Operands.size())
2356 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002357
Chris Lattnere73d4f82010-10-28 21:41:58 +00002358 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2359 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2360 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002361
Chris Lattnere73d4f82010-10-28 21:41:58 +00002362 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002363 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002364 case Match_MnemonicFail:
2365 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002366 case Match_ConversionFail:
2367 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002368 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002369
Eric Christopherc223e2b2010-10-29 09:26:59 +00002370 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002371 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002372}
2373
Kevin Enderby515d5092009-10-15 20:48:48 +00002374/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002375bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2376 StringRef IDVal = DirectiveID.getIdentifier();
2377 if (IDVal == ".word")
2378 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002379 else if (IDVal == ".thumb")
2380 return ParseDirectiveThumb(DirectiveID.getLoc());
2381 else if (IDVal == ".thumb_func")
2382 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2383 else if (IDVal == ".code")
2384 return ParseDirectiveCode(DirectiveID.getLoc());
2385 else if (IDVal == ".syntax")
2386 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002387 return true;
2388}
2389
2390/// ParseDirectiveWord
2391/// ::= .word [ expression (, expression)* ]
2392bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2393 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2394 for (;;) {
2395 const MCExpr *Value;
2396 if (getParser().ParseExpression(Value))
2397 return true;
2398
Chris Lattneraaec2052010-01-19 19:46:13 +00002399 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002400
2401 if (getLexer().is(AsmToken::EndOfStatement))
2402 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002403
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002404 // FIXME: Improve diagnostic.
2405 if (getLexer().isNot(AsmToken::Comma))
2406 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002407 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002408 }
2409 }
2410
Sean Callananb9a25b72010-01-19 20:27:46 +00002411 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002412 return false;
2413}
2414
Kevin Enderby515d5092009-10-15 20:48:48 +00002415/// ParseDirectiveThumb
2416/// ::= .thumb
2417bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2418 if (getLexer().isNot(AsmToken::EndOfStatement))
2419 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002420 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002421
2422 // TODO: set thumb mode
2423 // TODO: tell the MC streamer the mode
2424 // getParser().getStreamer().Emit???();
2425 return false;
2426}
2427
2428/// ParseDirectiveThumbFunc
2429/// ::= .thumbfunc symbol_name
2430bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002431 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2432 bool isMachO = MAI.hasSubsectionsViaSymbols();
2433 StringRef Name;
2434
2435 // Darwin asm has function name after .thumb_func direction
2436 // ELF doesn't
2437 if (isMachO) {
2438 const AsmToken &Tok = Parser.getTok();
2439 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2440 return Error(L, "unexpected token in .thumb_func directive");
2441 Name = Tok.getString();
2442 Parser.Lex(); // Consume the identifier token.
2443 }
2444
Kevin Enderby515d5092009-10-15 20:48:48 +00002445 if (getLexer().isNot(AsmToken::EndOfStatement))
2446 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002447 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002448
Rafael Espindola64695402011-05-16 16:17:21 +00002449 // FIXME: assuming function name will be the line following .thumb_func
2450 if (!isMachO) {
2451 Name = Parser.getTok().getString();
2452 }
2453
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002454 // Mark symbol as a thumb symbol.
2455 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2456 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002457 return false;
2458}
2459
2460/// ParseDirectiveSyntax
2461/// ::= .syntax unified | divided
2462bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002463 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002464 if (Tok.isNot(AsmToken::Identifier))
2465 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002466 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002467 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002468 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002469 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002470 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002471 else
2472 return Error(L, "unrecognized syntax mode in .syntax directive");
2473
2474 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002475 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002476 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002477
2478 // TODO tell the MC streamer the mode
2479 // getParser().getStreamer().Emit???();
2480 return false;
2481}
2482
2483/// ParseDirectiveCode
2484/// ::= .code 16 | 32
2485bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002486 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002487 if (Tok.isNot(AsmToken::Integer))
2488 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002489 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002490 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002491 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002492 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002493 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002494 else
2495 return Error(L, "invalid operand to .code directive");
2496
2497 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002498 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002499 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002500
Evan Cheng32869202011-07-08 22:36:29 +00002501 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002502 if (!isThumb())
2503 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002504 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002505 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002506 if (isThumb())
2507 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002508 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002509 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002510
Kevin Enderby515d5092009-10-15 20:48:48 +00002511 return false;
2512}
2513
Sean Callanan90b70972010-04-07 20:29:34 +00002514extern "C" void LLVMInitializeARMAsmLexer();
2515
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002516/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002517extern "C" void LLVMInitializeARMAsmParser() {
2518 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2519 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002520 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002521}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002522
Chris Lattner0692ee62010-09-06 19:11:01 +00002523#define GET_REGISTER_MATCHER
2524#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002525#include "ARMGenAsmMatcher.inc"