blob: 827ad0fb9f9ae49be87d2f57601eab37bba7978c [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 {
629 assert(N == 3 && "Invalid number of operands!");
630 assert(isShiftedImm() && "addShiftedImmOperands() on non ShiftedImm!");
631 Inst.addOperand(MCOperand::CreateReg(ShiftedImm.SrcReg));
632 if (ShiftedImm.ShiftTy == ARM_AM::rrx)
633 Inst.addOperand(MCOperand::CreateReg(ShiftedImm.SrcReg));
634 else
635 Inst.addOperand(MCOperand::CreateReg(0));
636 Inst.addOperand(MCOperand::CreateImm(
637 ARM_AM::getSORegOpc(ShiftedImm.ShiftTy, ShiftedImm.ShiftImm)));
638 }
639
640
Owen Anderson00828302011-03-18 22:50:18 +0000641 void addShifterOperands(MCInst &Inst, unsigned N) const {
642 assert(N == 1 && "Invalid number of operands!");
643 Inst.addOperand(MCOperand::CreateImm(
644 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
645 }
646
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000647 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000648 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000649 const SmallVectorImpl<unsigned> &RegList = getRegList();
650 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000651 I = RegList.begin(), E = RegList.end(); I != E; ++I)
652 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000653 }
654
Bill Wendling0f630752010-11-17 04:32:08 +0000655 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
656 addRegListOperands(Inst, N);
657 }
658
659 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
660 addRegListOperands(Inst, N);
661 }
662
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000663 void addImmOperands(MCInst &Inst, unsigned N) const {
664 assert(N == 1 && "Invalid number of operands!");
665 addExpr(Inst, getImm());
666 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000667
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000668 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
669 assert(N == 1 && "Invalid number of operands!");
670 addExpr(Inst, getImm());
671 }
672
Jim Grosbach83ab0702011-07-13 22:01:08 +0000673 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
674 assert(N == 1 && "Invalid number of operands!");
675 addExpr(Inst, getImm());
676 }
677
678 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
679 assert(N == 1 && "Invalid number of operands!");
680 addExpr(Inst, getImm());
681 }
682
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000683 void addImm0_31Operands(MCInst &Inst, unsigned N) const {
684 assert(N == 1 && "Invalid number of operands!");
685 addExpr(Inst, getImm());
686 }
687
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000688 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
689 assert(N == 1 && "Invalid number of operands!");
690 addExpr(Inst, getImm());
691 }
692
Jim Grosbachffa32252011-07-19 19:13:28 +0000693 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
694 assert(N == 1 && "Invalid number of operands!");
695 addExpr(Inst, getImm());
696 }
697
Jim Grosbachf6c05252011-07-21 17:23:04 +0000698 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
699 assert(N == 1 && "Invalid number of operands!");
700 addExpr(Inst, getImm());
701 }
702
703 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
704 assert(N == 1 && "Invalid number of operands!");
705 // An ASR value of 32 encodes as 0, so that's how we want to add it to
706 // the instruction as well.
707 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
708 int Val = CE->getValue();
709 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
710 }
711
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000712 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
713 assert(N == 1 && "Invalid number of operands!");
714 addExpr(Inst, getImm());
715 }
716
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000717 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
718 assert(N == 1 && "Invalid number of operands!");
719 addExpr(Inst, getImm());
720 }
721
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000722 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
723 assert(N == 1 && "Invalid number of operands!");
724 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
725 }
726
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000727 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
728 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
729 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
730
731 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000732 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000733 assert((CE || CE->getValue() == 0) &&
734 "No offset operand support in mode 7");
735 }
736
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000737 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
738 assert(isMemMode2() && "Invalid mode or number of operands!");
739 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
740 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
741
742 if (getMemOffsetIsReg()) {
743 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
744
745 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
746 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
747 int64_t ShiftAmount = 0;
748
749 if (getMemOffsetRegShifted()) {
750 ShOpc = getMemShiftType();
751 const MCConstantExpr *CE =
752 dyn_cast<MCConstantExpr>(getMemShiftAmount());
753 ShiftAmount = CE->getValue();
754 }
755
756 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
757 ShOpc, IdxMode)));
758 return;
759 }
760
761 // Create a operand placeholder to always yield the same number of operands.
762 Inst.addOperand(MCOperand::CreateReg(0));
763
764 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
765 // the difference?
766 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
767 assert(CE && "Non-constant mode 2 offset operand!");
768 int64_t Offset = CE->getValue();
769
770 if (Offset >= 0)
771 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
772 Offset, ARM_AM::no_shift, IdxMode)));
773 else
774 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
775 -Offset, ARM_AM::no_shift, IdxMode)));
776 }
777
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000778 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
779 assert(isMemMode3() && "Invalid mode or number of operands!");
780 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
781 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
782
783 if (getMemOffsetIsReg()) {
784 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
785
786 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
787 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
788 IdxMode)));
789 return;
790 }
791
792 // Create a operand placeholder to always yield the same number of operands.
793 Inst.addOperand(MCOperand::CreateReg(0));
794
795 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
796 // the difference?
797 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
798 assert(CE && "Non-constant mode 3 offset operand!");
799 int64_t Offset = CE->getValue();
800
801 if (Offset >= 0)
802 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
803 Offset, IdxMode)));
804 else
805 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
806 -Offset, IdxMode)));
807 }
808
Chris Lattner14b93852010-10-29 00:27:31 +0000809 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
810 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000811
Daniel Dunbar4b462672011-01-18 05:55:27 +0000812 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
813 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000814
Jim Grosbach80eb2332010-10-29 17:41:25 +0000815 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
816 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000817 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000818 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000819
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000820 // The MCInst offset operand doesn't include the low two bits (like
821 // the instruction encoding).
822 int64_t Offset = CE->getValue() / 4;
823 if (Offset >= 0)
824 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
825 Offset)));
826 else
827 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
828 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000829 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000830
Bill Wendlingf4caf692010-12-14 03:36:38 +0000831 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
832 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000833 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
834 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000835 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000836
Bill Wendlingf4caf692010-12-14 03:36:38 +0000837 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
838 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000839 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
840 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000841 assert(CE && "Non-constant mode offset operand!");
842 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000843 }
844
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000845 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
846 assert(N == 1 && "Invalid number of operands!");
847 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
848 }
849
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000850 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
851 assert(N == 1 && "Invalid number of operands!");
852 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
853 }
854
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000855 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000856
Chris Lattner3a697562010-10-28 17:20:03 +0000857 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
858 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000859 Op->CC.Val = CC;
860 Op->StartLoc = S;
861 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000862 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000863 }
864
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000865 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
866 ARMOperand *Op = new ARMOperand(CoprocNum);
867 Op->Cop.Val = CopVal;
868 Op->StartLoc = S;
869 Op->EndLoc = S;
870 return Op;
871 }
872
873 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
874 ARMOperand *Op = new ARMOperand(CoprocReg);
875 Op->Cop.Val = CopVal;
876 Op->StartLoc = S;
877 Op->EndLoc = S;
878 return Op;
879 }
880
Jim Grosbachd67641b2010-12-06 18:21:12 +0000881 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
882 ARMOperand *Op = new ARMOperand(CCOut);
883 Op->Reg.RegNum = RegNum;
884 Op->StartLoc = S;
885 Op->EndLoc = S;
886 return Op;
887 }
888
Chris Lattner3a697562010-10-28 17:20:03 +0000889 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
890 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000891 Op->Tok.Data = Str.data();
892 Op->Tok.Length = Str.size();
893 Op->StartLoc = S;
894 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000895 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000896 }
897
Bill Wendling50d0f582010-11-18 23:43:05 +0000898 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000899 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000900 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000901 Op->StartLoc = S;
902 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000903 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000904 }
905
Jim Grosbache8606dc2011-07-13 17:50:29 +0000906 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
907 unsigned SrcReg,
908 unsigned ShiftReg,
909 unsigned ShiftImm,
910 SMLoc S, SMLoc E) {
911 ARMOperand *Op = new ARMOperand(ShiftedRegister);
912 Op->ShiftedReg.ShiftTy = ShTy;
913 Op->ShiftedReg.SrcReg = SrcReg;
914 Op->ShiftedReg.ShiftReg = ShiftReg;
915 Op->ShiftedReg.ShiftImm = ShiftImm;
916 Op->StartLoc = S;
917 Op->EndLoc = E;
918 return Op;
919 }
920
Owen Anderson92a20222011-07-21 18:54:16 +0000921 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
922 unsigned SrcReg,
923 unsigned ShiftImm,
924 SMLoc S, SMLoc E) {
925 ARMOperand *Op = new ARMOperand(ShiftedImmediate);
926 Op->ShiftedImm.ShiftTy = ShTy;
927 Op->ShiftedImm.SrcReg = SrcReg;
928 Op->ShiftedImm.ShiftImm = ShiftImm;
929 Op->StartLoc = S;
930 Op->EndLoc = E;
931 return Op;
932 }
933
Owen Anderson00828302011-03-18 22:50:18 +0000934 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
935 SMLoc S, SMLoc E) {
936 ARMOperand *Op = new ARMOperand(Shifter);
937 Op->Shift.ShiftTy = ShTy;
938 Op->StartLoc = S;
939 Op->EndLoc = E;
940 return Op;
941 }
942
Bill Wendling7729e062010-11-09 22:44:22 +0000943 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000944 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000945 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000946 KindTy Kind = RegisterList;
947
948 if (ARM::DPRRegClass.contains(Regs.front().first))
949 Kind = DPRRegisterList;
950 else if (ARM::SPRRegClass.contains(Regs.front().first))
951 Kind = SPRRegisterList;
952
953 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000954 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000955 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000956 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000957 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000958 Op->StartLoc = StartLoc;
959 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000960 return Op;
961 }
962
Chris Lattner3a697562010-10-28 17:20:03 +0000963 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
964 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000965 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000966 Op->StartLoc = S;
967 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000968 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000969 }
970
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000971 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
972 bool OffsetIsReg, const MCExpr *Offset,
973 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000974 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000975 const MCExpr *ShiftAmount, bool Preindexed,
976 bool Postindexed, bool Negative, bool Writeback,
977 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000978 assert((OffsetRegNum == -1 || OffsetIsReg) &&
979 "OffsetRegNum must imply OffsetIsReg!");
980 assert((!OffsetRegShifted || OffsetIsReg) &&
981 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000982 assert((Offset || OffsetIsReg) &&
983 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000984 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
985 "Cannot have shift amount without shifted register offset!");
986 assert((!Offset || !OffsetIsReg) &&
987 "Cannot have expression offset and register offset!");
988
Chris Lattner3a697562010-10-28 17:20:03 +0000989 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000990 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000991 Op->Mem.BaseRegNum = BaseRegNum;
992 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000993 if (OffsetIsReg)
994 Op->Mem.Offset.RegNum = OffsetRegNum;
995 else
996 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000997 Op->Mem.OffsetRegShifted = OffsetRegShifted;
998 Op->Mem.ShiftType = ShiftType;
999 Op->Mem.ShiftAmount = ShiftAmount;
1000 Op->Mem.Preindexed = Preindexed;
1001 Op->Mem.Postindexed = Postindexed;
1002 Op->Mem.Negative = Negative;
1003 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +00001004
Sean Callanan76264762010-04-02 22:27:05 +00001005 Op->StartLoc = S;
1006 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001007 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001008 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001009
1010 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
1011 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
1012 Op->MBOpt.Val = Opt;
1013 Op->StartLoc = S;
1014 Op->EndLoc = S;
1015 return Op;
1016 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001017
1018 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1019 ARMOperand *Op = new ARMOperand(ProcIFlags);
1020 Op->IFlags.Val = IFlags;
1021 Op->StartLoc = S;
1022 Op->EndLoc = S;
1023 return Op;
1024 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001025
1026 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1027 ARMOperand *Op = new ARMOperand(MSRMask);
1028 Op->MMask.Val = MMask;
1029 Op->StartLoc = S;
1030 Op->EndLoc = S;
1031 return Op;
1032 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001033};
1034
1035} // end anonymous namespace.
1036
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001037void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001038 switch (Kind) {
1039 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +00001040 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001041 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +00001042 case CCOut:
1043 OS << "<ccout " << getReg() << ">";
1044 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001045 case CoprocNum:
1046 OS << "<coprocessor number: " << getCoproc() << ">";
1047 break;
1048 case CoprocReg:
1049 OS << "<coprocessor register: " << getCoproc() << ">";
1050 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001051 case MSRMask:
1052 OS << "<mask: " << getMSRMask() << ">";
1053 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001054 case Immediate:
1055 getImm()->print(OS);
1056 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001057 case MemBarrierOpt:
1058 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1059 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001060 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001061 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001062 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
1063 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001064 if (getMemOffsetIsReg()) {
1065 OS << " offset:<register " << getMemOffsetRegNum();
1066 if (getMemOffsetRegShifted()) {
1067 OS << " offset-shift-type:" << getMemShiftType();
1068 OS << " offset-shift-amount:" << *getMemShiftAmount();
1069 }
1070 } else {
1071 OS << " offset:" << *getMemOffset();
1072 }
1073 if (getMemOffsetIsReg())
1074 OS << " (offset-is-reg)";
1075 if (getMemPreindexed())
1076 OS << " (pre-indexed)";
1077 if (getMemPostindexed())
1078 OS << " (post-indexed)";
1079 if (getMemNegative())
1080 OS << " (negative)";
1081 if (getMemWriteback())
1082 OS << " (writeback)";
1083 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001084 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001085 case ProcIFlags: {
1086 OS << "<ARM_PROC::";
1087 unsigned IFlags = getProcIFlags();
1088 for (int i=2; i >= 0; --i)
1089 if (IFlags & (1 << i))
1090 OS << ARM_PROC::IFlagsToString(1 << i);
1091 OS << ">";
1092 break;
1093 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001094 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +00001095 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001096 break;
Owen Anderson00828302011-03-18 22:50:18 +00001097 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +00001098 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
1099 break;
1100 case ShiftedRegister:
Owen Anderson92a20222011-07-21 18:54:16 +00001101 OS << "<so_reg_reg "
Jim Grosbache8606dc2011-07-13 17:50:29 +00001102 << ShiftedReg.SrcReg
1103 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
1104 << ", " << ShiftedReg.ShiftReg << ", "
1105 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
1106 << ">";
Owen Anderson00828302011-03-18 22:50:18 +00001107 break;
Owen Anderson92a20222011-07-21 18:54:16 +00001108 case ShiftedImmediate:
1109 OS << "<so_reg_imm "
1110 << ShiftedImm.SrcReg
1111 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedImm.ShiftImm))
1112 << ", " << ARM_AM::getSORegOffset(ShiftedImm.ShiftImm)
1113 << ">";
1114 break;
Bill Wendling0f630752010-11-17 04:32:08 +00001115 case RegisterList:
1116 case DPRRegisterList:
1117 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00001118 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001119
Bill Wendling5fa22a12010-11-09 23:28:44 +00001120 const SmallVectorImpl<unsigned> &RegList = getRegList();
1121 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001122 I = RegList.begin(), E = RegList.end(); I != E; ) {
1123 OS << *I;
1124 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001125 }
1126
1127 OS << ">";
1128 break;
1129 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001130 case Token:
1131 OS << "'" << getToken() << "'";
1132 break;
1133 }
1134}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001135
1136/// @name Auto-generated Match Functions
1137/// {
1138
1139static unsigned MatchRegisterName(StringRef Name);
1140
1141/// }
1142
Bob Wilson69df7232011-02-03 21:46:10 +00001143bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1144 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +00001145 RegNo = TryParseRegister();
1146
1147 return (RegNo == (unsigned)-1);
1148}
1149
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001150/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00001151/// and if it is a register name the token is eaten and the register number is
1152/// returned. Otherwise return -1.
1153///
1154int ARMAsmParser::TryParseRegister() {
1155 const AsmToken &Tok = Parser.getTok();
1156 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001157
Chris Lattnere5658fa2010-10-30 04:09:10 +00001158 // FIXME: Validate register for the current architecture; we have to do
1159 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001160 std::string upperCase = Tok.getString().str();
1161 std::string lowerCase = LowercaseString(upperCase);
1162 unsigned RegNum = MatchRegisterName(lowerCase);
1163 if (!RegNum) {
1164 RegNum = StringSwitch<unsigned>(lowerCase)
1165 .Case("r13", ARM::SP)
1166 .Case("r14", ARM::LR)
1167 .Case("r15", ARM::PC)
1168 .Case("ip", ARM::R12)
1169 .Default(0);
1170 }
1171 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001172
Chris Lattnere5658fa2010-10-30 04:09:10 +00001173 Parser.Lex(); // Eat identifier token.
1174 return RegNum;
1175}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001176
Jim Grosbach19906722011-07-13 18:49:30 +00001177// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1178// If a recoverable error occurs, return 1. If an irrecoverable error
1179// occurs, return -1. An irrecoverable error is one where tokens have been
1180// consumed in the process of trying to parse the shifter (i.e., when it is
1181// indeed a shifter operand, but malformed).
1182int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001183 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1184 SMLoc S = Parser.getTok().getLoc();
1185 const AsmToken &Tok = Parser.getTok();
1186 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1187
1188 std::string upperCase = Tok.getString().str();
1189 std::string lowerCase = LowercaseString(upperCase);
1190 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1191 .Case("lsl", ARM_AM::lsl)
1192 .Case("lsr", ARM_AM::lsr)
1193 .Case("asr", ARM_AM::asr)
1194 .Case("ror", ARM_AM::ror)
1195 .Case("rrx", ARM_AM::rrx)
1196 .Default(ARM_AM::no_shift);
1197
1198 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001199 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001200
Jim Grosbache8606dc2011-07-13 17:50:29 +00001201 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001202
Jim Grosbache8606dc2011-07-13 17:50:29 +00001203 // The source register for the shift has already been added to the
1204 // operand list, so we need to pop it off and combine it into the shifted
1205 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00001206 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00001207 if (!PrevOp->isReg())
1208 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1209 int SrcReg = PrevOp->getReg();
1210 int64_t Imm = 0;
1211 int ShiftReg = 0;
1212 if (ShiftTy == ARM_AM::rrx) {
1213 // RRX Doesn't have an explicit shift amount. The encoder expects
1214 // the shift register to be the same as the source register. Seems odd,
1215 // but OK.
1216 ShiftReg = SrcReg;
1217 } else {
1218 // Figure out if this is shifted by a constant or a register (for non-RRX).
1219 if (Parser.getTok().is(AsmToken::Hash)) {
1220 Parser.Lex(); // Eat hash.
1221 SMLoc ImmLoc = Parser.getTok().getLoc();
1222 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001223 if (getParser().ParseExpression(ShiftExpr)) {
1224 Error(ImmLoc, "invalid immediate shift value");
1225 return -1;
1226 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001227 // The expression must be evaluatable as an immediate.
1228 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001229 if (!CE) {
1230 Error(ImmLoc, "invalid immediate shift value");
1231 return -1;
1232 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001233 // Range check the immediate.
1234 // lsl, ror: 0 <= imm <= 31
1235 // lsr, asr: 0 <= imm <= 32
1236 Imm = CE->getValue();
1237 if (Imm < 0 ||
1238 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1239 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001240 Error(ImmLoc, "immediate shift value out of range");
1241 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001242 }
1243 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1244 ShiftReg = TryParseRegister();
1245 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001246 if (ShiftReg == -1) {
1247 Error (L, "expected immediate or register in shift operand");
1248 return -1;
1249 }
1250 } else {
1251 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001252 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001253 return -1;
1254 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001255 }
1256
Owen Anderson92a20222011-07-21 18:54:16 +00001257 if (ShiftReg && ShiftTy != ARM_AM::rrx)
1258 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
Jim Grosbache8606dc2011-07-13 17:50:29 +00001259 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001260 S, Parser.getTok().getLoc()));
Owen Anderson92a20222011-07-21 18:54:16 +00001261 else
1262 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
1263 S, Parser.getTok().getLoc()));
Owen Anderson00828302011-03-18 22:50:18 +00001264
Jim Grosbach19906722011-07-13 18:49:30 +00001265 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001266}
1267
1268
Bill Wendling50d0f582010-11-18 23:43:05 +00001269/// Try to parse a register name. The token must be an Identifier when called.
1270/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1271/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001272///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001273/// TODO this is likely to change to allow different register types and or to
1274/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001275bool ARMAsmParser::
1276TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001277 SMLoc S = Parser.getTok().getLoc();
1278 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001279 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001280 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001281
Bill Wendling50d0f582010-11-18 23:43:05 +00001282 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001283
Chris Lattnere5658fa2010-10-30 04:09:10 +00001284 const AsmToken &ExclaimTok = Parser.getTok();
1285 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001286 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1287 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001288 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001289 }
1290
Bill Wendling50d0f582010-11-18 23:43:05 +00001291 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001292}
1293
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001294/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1295/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1296/// "c5", ...
1297static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001298 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1299 // but efficient.
1300 switch (Name.size()) {
1301 default: break;
1302 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001303 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001304 return -1;
1305 switch (Name[1]) {
1306 default: return -1;
1307 case '0': return 0;
1308 case '1': return 1;
1309 case '2': return 2;
1310 case '3': return 3;
1311 case '4': return 4;
1312 case '5': return 5;
1313 case '6': return 6;
1314 case '7': return 7;
1315 case '8': return 8;
1316 case '9': return 9;
1317 }
1318 break;
1319 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001320 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001321 return -1;
1322 switch (Name[2]) {
1323 default: return -1;
1324 case '0': return 10;
1325 case '1': return 11;
1326 case '2': return 12;
1327 case '3': return 13;
1328 case '4': return 14;
1329 case '5': return 15;
1330 }
1331 break;
1332 }
1333
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001334 return -1;
1335}
1336
Jim Grosbachf922c472011-02-12 01:34:40 +00001337/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001338/// token must be an Identifier when called, and if it is a coprocessor
1339/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001340ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1341tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001342 SMLoc S = Parser.getTok().getLoc();
1343 const AsmToken &Tok = Parser.getTok();
1344 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1345
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001346 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001347 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001348 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001349
1350 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001351 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001352 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001353}
1354
Jim Grosbachf922c472011-02-12 01:34:40 +00001355/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001356/// token must be an Identifier when called, and if it is a coprocessor
1357/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001358ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1359tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001360 SMLoc S = Parser.getTok().getLoc();
1361 const AsmToken &Tok = Parser.getTok();
1362 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1363
1364 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1365 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001366 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001367
1368 Parser.Lex(); // Eat identifier token.
1369 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001370 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001371}
1372
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001373/// Parse a register list, return it if successful else return null. The first
1374/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001375bool ARMAsmParser::
1376ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001377 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001378 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001379 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001380
Bill Wendling7729e062010-11-09 22:44:22 +00001381 // Read the rest of the registers in the list.
1382 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001383 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001384
Bill Wendling7729e062010-11-09 22:44:22 +00001385 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001386 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001387 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001388
Sean Callanan18b83232010-01-19 21:44:56 +00001389 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001390 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001391 if (RegTok.isNot(AsmToken::Identifier)) {
1392 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001393 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001394 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001395
Bill Wendling1d6a2652010-11-06 10:40:24 +00001396 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001397 if (RegNum == -1) {
1398 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001399 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001400 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001401
Bill Wendlinge7176102010-11-06 22:36:58 +00001402 if (IsRange) {
1403 int Reg = PrevRegNum;
1404 do {
1405 ++Reg;
1406 Registers.push_back(std::make_pair(Reg, RegLoc));
1407 } while (Reg != RegNum);
1408 } else {
1409 Registers.push_back(std::make_pair(RegNum, RegLoc));
1410 }
1411
1412 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001413 } while (Parser.getTok().is(AsmToken::Comma) ||
1414 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001415
1416 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001417 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001418 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1419 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001420 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001421 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001422
Bill Wendlinge7176102010-11-06 22:36:58 +00001423 SMLoc E = RCurlyTok.getLoc();
1424 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001425
Bill Wendlinge7176102010-11-06 22:36:58 +00001426 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001427 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001428 RI = Registers.begin(), RE = Registers.end();
1429
Bill Wendling7caebff2011-01-12 21:20:59 +00001430 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001431 bool EmittedWarning = false;
1432
Bill Wendling7caebff2011-01-12 21:20:59 +00001433 DenseMap<unsigned, bool> RegMap;
1434 RegMap[HighRegNum] = true;
1435
Bill Wendlinge7176102010-11-06 22:36:58 +00001436 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001437 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001438 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001439
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001440 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001441 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001442 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001443 }
1444
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001445 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001446 Warning(RegInfo.second,
1447 "register not in ascending order in register list");
1448
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001449 RegMap[Reg] = true;
1450 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001451 }
1452
Bill Wendling50d0f582010-11-18 23:43:05 +00001453 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1454 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001455}
1456
Jim Grosbachf922c472011-02-12 01:34:40 +00001457/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1458ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1459tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001460 SMLoc S = Parser.getTok().getLoc();
1461 const AsmToken &Tok = Parser.getTok();
1462 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1463 StringRef OptStr = Tok.getString();
1464
1465 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1466 .Case("sy", ARM_MB::SY)
1467 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001468 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001469 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001470 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001471 .Case("ishst", ARM_MB::ISHST)
1472 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001473 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001474 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001475 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001476 .Case("osh", ARM_MB::OSH)
1477 .Case("oshst", ARM_MB::OSHST)
1478 .Default(~0U);
1479
1480 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001481 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001482
1483 Parser.Lex(); // Eat identifier token.
1484 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001485 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001486}
1487
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001488/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001489ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1490tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1491 SMLoc S = Parser.getTok().getLoc();
1492 const AsmToken &Tok = Parser.getTok();
1493 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1494 StringRef IFlagsStr = Tok.getString();
1495
1496 unsigned IFlags = 0;
1497 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1498 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1499 .Case("a", ARM_PROC::A)
1500 .Case("i", ARM_PROC::I)
1501 .Case("f", ARM_PROC::F)
1502 .Default(~0U);
1503
1504 // If some specific iflag is already set, it means that some letter is
1505 // present more than once, this is not acceptable.
1506 if (Flag == ~0U || (IFlags & Flag))
1507 return MatchOperand_NoMatch;
1508
1509 IFlags |= Flag;
1510 }
1511
1512 Parser.Lex(); // Eat identifier token.
1513 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1514 return MatchOperand_Success;
1515}
1516
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001517/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1518ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1519tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1520 SMLoc S = Parser.getTok().getLoc();
1521 const AsmToken &Tok = Parser.getTok();
1522 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1523 StringRef Mask = Tok.getString();
1524
1525 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1526 size_t Start = 0, Next = Mask.find('_');
1527 StringRef Flags = "";
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00001528 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001529 if (Next != StringRef::npos)
1530 Flags = Mask.slice(Next+1, Mask.size());
1531
1532 // FlagsVal contains the complete mask:
1533 // 3-0: Mask
1534 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1535 unsigned FlagsVal = 0;
1536
1537 if (SpecReg == "apsr") {
1538 FlagsVal = StringSwitch<unsigned>(Flags)
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00001539 .Case("nzcvq", 0x8) // same as CPSR_f
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001540 .Case("g", 0x4) // same as CPSR_s
1541 .Case("nzcvqg", 0xc) // same as CPSR_fs
1542 .Default(~0U);
1543
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001544 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001545 if (!Flags.empty())
1546 return MatchOperand_NoMatch;
1547 else
1548 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001549 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001550 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001551 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1552 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001553 for (int i = 0, e = Flags.size(); i != e; ++i) {
1554 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1555 .Case("c", 1)
1556 .Case("x", 2)
1557 .Case("s", 4)
1558 .Case("f", 8)
1559 .Default(~0U);
1560
1561 // If some specific flag is already set, it means that some letter is
1562 // present more than once, this is not acceptable.
1563 if (FlagsVal == ~0U || (FlagsVal & Flag))
1564 return MatchOperand_NoMatch;
1565 FlagsVal |= Flag;
1566 }
1567 } else // No match for special register.
1568 return MatchOperand_NoMatch;
1569
1570 // Special register without flags are equivalent to "fc" flags.
1571 if (!FlagsVal)
1572 FlagsVal = 0x9;
1573
1574 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1575 if (SpecReg == "spsr")
1576 FlagsVal |= 16;
1577
1578 Parser.Lex(); // Eat identifier token.
1579 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1580 return MatchOperand_Success;
1581}
1582
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001583/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1584ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1585tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001586 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001587
1588 if (ParseMemory(Operands, ARMII::AddrMode2))
1589 return MatchOperand_NoMatch;
1590
1591 return MatchOperand_Success;
1592}
1593
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001594/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1595ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1596tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1597 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1598
1599 if (ParseMemory(Operands, ARMII::AddrMode3))
1600 return MatchOperand_NoMatch;
1601
1602 return MatchOperand_Success;
1603}
1604
Jim Grosbachf6c05252011-07-21 17:23:04 +00001605ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1606parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
1607 int Low, int High) {
1608 const AsmToken &Tok = Parser.getTok();
1609 if (Tok.isNot(AsmToken::Identifier)) {
1610 Error(Parser.getTok().getLoc(), Op + " operand expected.");
1611 return MatchOperand_ParseFail;
1612 }
1613 StringRef ShiftName = Tok.getString();
1614 std::string LowerOp = LowercaseString(Op);
1615 std::string UpperOp = UppercaseString(Op);
1616 if (ShiftName != LowerOp && ShiftName != UpperOp) {
1617 Error(Parser.getTok().getLoc(), Op + " operand expected.");
1618 return MatchOperand_ParseFail;
1619 }
1620 Parser.Lex(); // Eat shift type token.
1621
1622 // There must be a '#' and a shift amount.
1623 if (Parser.getTok().isNot(AsmToken::Hash)) {
1624 Error(Parser.getTok().getLoc(), "'#' expected");
1625 return MatchOperand_ParseFail;
1626 }
1627 Parser.Lex(); // Eat hash token.
1628
1629 const MCExpr *ShiftAmount;
1630 SMLoc Loc = Parser.getTok().getLoc();
1631 if (getParser().ParseExpression(ShiftAmount)) {
1632 Error(Loc, "illegal expression");
1633 return MatchOperand_ParseFail;
1634 }
1635 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
1636 if (!CE) {
1637 Error(Loc, "constant expression expected");
1638 return MatchOperand_ParseFail;
1639 }
1640 int Val = CE->getValue();
1641 if (Val < Low || Val > High) {
1642 Error(Loc, "immediate value out of range");
1643 return MatchOperand_ParseFail;
1644 }
1645
1646 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
1647
1648 return MatchOperand_Success;
1649}
1650
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001651/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1652/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1653/// when they refer multiple MIOperands inside a single one.
1654bool ARMAsmParser::
1655CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1656 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1657 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1658
1659 // Create a writeback register dummy placeholder.
1660 Inst.addOperand(MCOperand::CreateImm(0));
1661
1662 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1663 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1664 return true;
1665}
1666
1667/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1668/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1669/// when they refer multiple MIOperands inside a single one.
1670bool ARMAsmParser::
1671CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1672 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1673 // Create a writeback register dummy placeholder.
1674 Inst.addOperand(MCOperand::CreateImm(0));
1675 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1676 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1677 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1678 return true;
1679}
1680
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001681/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1682/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1683/// when they refer multiple MIOperands inside a single one.
1684bool ARMAsmParser::
1685CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1686 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1687 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1688
1689 // Create a writeback register dummy placeholder.
1690 Inst.addOperand(MCOperand::CreateImm(0));
1691
1692 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1693 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1694 return true;
1695}
1696
1697/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1698/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1699/// when they refer multiple MIOperands inside a single one.
1700bool ARMAsmParser::
1701CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1702 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1703 // Create a writeback register dummy placeholder.
1704 Inst.addOperand(MCOperand::CreateImm(0));
1705 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1706 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1707 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1708 return true;
1709}
1710
Bill Wendlinge7176102010-11-06 22:36:58 +00001711/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001712/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001713///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001714/// TODO Only preindexing and postindexing addressing are started, unindexed
1715/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001716bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001717ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1718 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001719 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001720 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001721 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001722 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001723 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001724
Sean Callanan18b83232010-01-19 21:44:56 +00001725 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001726 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1727 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001728 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001729 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001730 int BaseRegNum = TryParseRegister();
1731 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001732 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001733 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001734 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001735
Daniel Dunbar05710932011-01-18 05:34:17 +00001736 // The next token must either be a comma or a closing bracket.
1737 const AsmToken &Tok = Parser.getTok();
1738 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1739 return true;
1740
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001741 bool Preindexed = false;
1742 bool Postindexed = false;
1743 bool OffsetIsReg = false;
1744 bool Negative = false;
1745 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001746 ARMOperand *WBOp = 0;
1747 int OffsetRegNum = -1;
1748 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001749 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001750 const MCExpr *ShiftAmount = 0;
1751 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001752
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001753 // First look for preindexed address forms, that is after the "[Rn" we now
1754 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001755 if (Tok.is(AsmToken::Comma)) {
1756 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001757 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001758
Chris Lattner550276e2010-10-28 20:52:15 +00001759 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1760 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001761 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001762 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001763 if (RBracTok.isNot(AsmToken::RBrac)) {
1764 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001765 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001766 }
Sean Callanan76264762010-04-02 22:27:05 +00001767 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001768 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001769
Sean Callanan18b83232010-01-19 21:44:56 +00001770 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001771 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001772 // None of addrmode3 instruction uses "!"
1773 if (AddrMode == ARMII::AddrMode3)
1774 return true;
1775
Bill Wendling50d0f582010-11-18 23:43:05 +00001776 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1777 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001778 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001779 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001780 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1781 if (AddrMode == ARMII::AddrMode2)
1782 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001783 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001784 } else {
1785 // The "[Rn" we have so far was not followed by a comma.
1786
Jim Grosbach80eb2332010-10-29 17:41:25 +00001787 // If there's anything other than the right brace, this is a post indexing
1788 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001789 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001790 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001791
Sean Callanan18b83232010-01-19 21:44:56 +00001792 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001793
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001794 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001795 Postindexed = true;
1796 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001797
Chris Lattner550276e2010-10-28 20:52:15 +00001798 if (NextTok.isNot(AsmToken::Comma)) {
1799 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001800 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001801 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001802
Sean Callananb9a25b72010-01-19 20:27:46 +00001803 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001804
Chris Lattner550276e2010-10-28 20:52:15 +00001805 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001806 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001807 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001808 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001809 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001810 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001811
1812 // Force Offset to exist if used.
1813 if (!OffsetIsReg) {
1814 if (!Offset)
1815 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001816 } else {
1817 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1818 Error(E, "shift amount not supported");
1819 return true;
1820 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001821 }
1822
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001823 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1824 Offset, OffsetRegNum, OffsetRegShifted,
1825 ShiftType, ShiftAmount, Preindexed,
1826 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001827 if (WBOp)
1828 Operands.push_back(WBOp);
1829
1830 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001831}
1832
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001833/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1834/// we will parse the following (were +/- means that a plus or minus is
1835/// optional):
1836/// +/-Rm
1837/// +/-Rm, shift
1838/// #offset
1839/// we return false on success or an error otherwise.
1840bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001841 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001842 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001843 const MCExpr *&ShiftAmount,
1844 const MCExpr *&Offset,
1845 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001846 int &OffsetRegNum,
1847 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001848 Negative = false;
1849 OffsetRegShifted = false;
1850 OffsetIsReg = false;
1851 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001852 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001853 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001854 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001855 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001856 else if (NextTok.is(AsmToken::Minus)) {
1857 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001858 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001859 }
1860 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001861 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001862 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001863 SMLoc CurLoc = OffsetRegTok.getLoc();
1864 OffsetRegNum = TryParseRegister();
1865 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001866 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001867 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001868 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001869 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001870
Bill Wendling12f40e92010-11-06 10:51:53 +00001871 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001872 if (OffsetRegNum != -1) {
1873 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001874 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001875 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001876 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001877
Sean Callanan18b83232010-01-19 21:44:56 +00001878 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001879 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001880 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001881 OffsetRegShifted = true;
1882 }
1883 }
1884 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1885 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001886 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001887 if (HashTok.isNot(AsmToken::Hash))
1888 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001889
Sean Callananb9a25b72010-01-19 20:27:46 +00001890 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001891
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001892 if (getParser().ParseExpression(Offset))
1893 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001894 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001895 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001896 return false;
1897}
1898
1899/// ParseShift as one of these two:
1900/// ( lsl | lsr | asr | ror ) , # shift_amount
1901/// rrx
1902/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001903bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1904 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001905 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001906 if (Tok.isNot(AsmToken::Identifier))
1907 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001908 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001909 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001910 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001911 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001912 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001913 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001914 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001915 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001916 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001917 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001918 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001919 else
1920 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001921 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001922
1923 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001924 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001925 return false;
1926
1927 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001928 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001929 if (HashTok.isNot(AsmToken::Hash))
1930 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001931 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001932
1933 if (getParser().ParseExpression(ShiftAmount))
1934 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001935
1936 return false;
1937}
1938
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001939/// Parse a arm instruction operand. For now this parses the operand regardless
1940/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001941bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001942 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001943 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001944
1945 // Check if the current operand has a custom associated parser, if so, try to
1946 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001947 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1948 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001949 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001950 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1951 // there was a match, but an error occurred, in which case, just return that
1952 // the operand parsing failed.
1953 if (ResTy == MatchOperand_ParseFail)
1954 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001955
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001956 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001957 default:
1958 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001959 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001960 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001961 if (!TryParseRegisterWithWriteBack(Operands))
1962 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001963 int Res = TryParseShiftRegister(Operands);
1964 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001965 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001966 else if (Res == -1) // irrecoverable error
1967 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001968
1969 // Fall though for the Identifier case that is not a register or a
1970 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00001971 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00001972 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1973 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001974 // This was not a register so parse other operands that start with an
1975 // identifier (like labels) as expressions and create them as immediates.
1976 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001977 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001978 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001979 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001980 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001981 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1982 return false;
1983 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001984 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001985 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001986 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001987 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001988 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001989 // #42 -> immediate.
1990 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001991 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001992 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001993 const MCExpr *ImmVal;
1994 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001995 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001996 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001997 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1998 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001999 case AsmToken::Colon: {
2000 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00002001 // FIXME: Check it's an expression prefix,
2002 // e.g. (FOO - :lower16:BAR) isn't legal.
2003 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002004 if (ParsePrefix(RefKind))
2005 return true;
2006
Evan Cheng75972122011-01-13 07:58:56 +00002007 const MCExpr *SubExprVal;
2008 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00002009 return true;
2010
Evan Cheng75972122011-01-13 07:58:56 +00002011 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
2012 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00002013 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00002014 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00002015 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002016 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00002017 }
2018}
2019
Evan Cheng75972122011-01-13 07:58:56 +00002020// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
2021// :lower16: and :upper16:.
2022bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
2023 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002024
2025 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00002026 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00002027 Parser.Lex(); // Eat ':'
2028
2029 if (getLexer().isNot(AsmToken::Identifier)) {
2030 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
2031 return true;
2032 }
2033
2034 StringRef IDVal = Parser.getTok().getIdentifier();
2035 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00002036 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002037 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00002038 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002039 } else {
2040 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
2041 return true;
2042 }
2043 Parser.Lex();
2044
2045 if (getLexer().isNot(AsmToken::Colon)) {
2046 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
2047 return true;
2048 }
2049 Parser.Lex(); // Eat the last ':'
2050 return false;
2051}
2052
2053const MCExpr *
2054ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
2055 MCSymbolRefExpr::VariantKind Variant) {
2056 // Recurse over the given expression, rebuilding it to apply the given variant
2057 // to the leftmost symbol.
2058 if (Variant == MCSymbolRefExpr::VK_None)
2059 return E;
2060
2061 switch (E->getKind()) {
2062 case MCExpr::Target:
2063 llvm_unreachable("Can't handle target expr yet");
2064 case MCExpr::Constant:
2065 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
2066
2067 case MCExpr::SymbolRef: {
2068 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
2069
2070 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
2071 return 0;
2072
2073 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
2074 }
2075
2076 case MCExpr::Unary:
2077 llvm_unreachable("Can't handle unary expressions yet");
2078
2079 case MCExpr::Binary: {
2080 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
2081 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
2082 const MCExpr *RHS = BE->getRHS();
2083 if (!LHS)
2084 return 0;
2085
2086 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
2087 }
2088 }
2089
2090 assert(0 && "Invalid expression kind!");
2091 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002092}
2093
Daniel Dunbar352e1482011-01-11 15:59:50 +00002094/// \brief Given a mnemonic, split out possible predication code and carry
2095/// setting letters to form a canonical mnemonic and flags.
2096//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002097// FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00002098StringRef ARMAsmParser::SplitMnemonic(StringRef Mnemonic,
2099 unsigned &PredicationCode,
2100 bool &CarrySetting,
2101 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00002102 PredicationCode = ARMCC::AL;
2103 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002104 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002105
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002106 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00002107 //
2108 // FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00002109 if ((Mnemonic == "movs" && isThumb()) ||
2110 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
2111 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
2112 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
2113 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
2114 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
2115 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
2116 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
Daniel Dunbar352e1482011-01-11 15:59:50 +00002117 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00002118
Jim Grosbach3f00e312011-07-11 17:09:57 +00002119 // First, split out any predication code. Ignore mnemonics we know aren't
2120 // predicated but do have a carry-set and so weren't caught above.
Jim Grosbachab40f4b2011-07-20 18:20:31 +00002121 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
2122 Mnemonic != "muls") {
Jim Grosbach3f00e312011-07-11 17:09:57 +00002123 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
2124 .Case("eq", ARMCC::EQ)
2125 .Case("ne", ARMCC::NE)
2126 .Case("hs", ARMCC::HS)
2127 .Case("cs", ARMCC::HS)
2128 .Case("lo", ARMCC::LO)
2129 .Case("cc", ARMCC::LO)
2130 .Case("mi", ARMCC::MI)
2131 .Case("pl", ARMCC::PL)
2132 .Case("vs", ARMCC::VS)
2133 .Case("vc", ARMCC::VC)
2134 .Case("hi", ARMCC::HI)
2135 .Case("ls", ARMCC::LS)
2136 .Case("ge", ARMCC::GE)
2137 .Case("lt", ARMCC::LT)
2138 .Case("gt", ARMCC::GT)
2139 .Case("le", ARMCC::LE)
2140 .Case("al", ARMCC::AL)
2141 .Default(~0U);
2142 if (CC != ~0U) {
2143 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
2144 PredicationCode = CC;
2145 }
Bill Wendling52925b62010-10-29 23:50:21 +00002146 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002147
Daniel Dunbar352e1482011-01-11 15:59:50 +00002148 // Next, determine if we have a carry setting bit. We explicitly ignore all
2149 // the instructions we know end in 's'.
2150 if (Mnemonic.endswith("s") &&
2151 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002152 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
2153 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
2154 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
2155 Mnemonic == "vrsqrts" || (Mnemonic == "movs" && isThumb()))) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00002156 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
2157 CarrySetting = true;
2158 }
2159
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002160 // The "cps" instruction can have a interrupt mode operand which is glued into
2161 // the mnemonic. Check if this is the case, split it and parse the imod op
2162 if (Mnemonic.startswith("cps")) {
2163 // Split out any imod code.
2164 unsigned IMod =
2165 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
2166 .Case("ie", ARM_PROC::IE)
2167 .Case("id", ARM_PROC::ID)
2168 .Default(~0U);
2169 if (IMod != ~0U) {
2170 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2171 ProcessorIMod = IMod;
2172 }
2173 }
2174
Daniel Dunbar352e1482011-01-11 15:59:50 +00002175 return Mnemonic;
2176}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002177
2178/// \brief Given a canonical mnemonic, determine if the instruction ever allows
2179/// inclusion of carry set or predication code operands.
2180//
2181// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00002182void ARMAsmParser::
2183GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2184 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002185 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2186 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2187 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2188 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002189 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002190 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2191 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002192 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00002193 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002194 CanAcceptCarrySet = true;
2195 } else {
2196 CanAcceptCarrySet = false;
2197 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002198
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002199 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2200 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2201 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2202 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002203 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "clrex" ||
2204 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumb())) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002205 CanAcceptPredicationCode = false;
2206 } else {
2207 CanAcceptPredicationCode = true;
2208 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002209
Evan Chengebdeeab2011-07-08 01:53:10 +00002210 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002211 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002212 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002213 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002214}
2215
2216/// Parse an arm instruction mnemonic followed by its operands.
2217bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2218 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2219 // Create the leading tokens for the mnemonic, split by '.' characters.
2220 size_t Start = 0, Next = Name.find('.');
Jim Grosbachffa32252011-07-19 19:13:28 +00002221 StringRef Mnemonic = Name.slice(Start, Next);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002222
Daniel Dunbar352e1482011-01-11 15:59:50 +00002223 // Split out the predication code and carry setting flag from the mnemonic.
2224 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002225 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002226 bool CarrySetting;
Jim Grosbachffa32252011-07-19 19:13:28 +00002227 Mnemonic = SplitMnemonic(Mnemonic, PredicationCode, CarrySetting,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002228 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002229
Jim Grosbachffa32252011-07-19 19:13:28 +00002230 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
2231
2232 // FIXME: This is all a pretty gross hack. We should automatically handle
2233 // optional operands like this via tblgen.
Bill Wendling9717fa92010-11-21 10:56:05 +00002234
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002235 // Next, add the CCOut and ConditionCode operands, if needed.
2236 //
2237 // For mnemonics which can ever incorporate a carry setting bit or predication
2238 // code, our matching model involves us always generating CCOut and
2239 // ConditionCode operands to match the mnemonic "as written" and then we let
2240 // the matcher deal with finding the right instruction or generating an
2241 // appropriate error.
2242 bool CanAcceptCarrySet, CanAcceptPredicationCode;
Jim Grosbachffa32252011-07-19 19:13:28 +00002243 GetMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002244
Jim Grosbach33c16a22011-07-14 22:04:21 +00002245 // If we had a carry-set on an instruction that can't do that, issue an
2246 // error.
2247 if (!CanAcceptCarrySet && CarrySetting) {
2248 Parser.EatToEndOfStatement();
Jim Grosbachffa32252011-07-19 19:13:28 +00002249 return Error(NameLoc, "instruction '" + Mnemonic +
Jim Grosbach33c16a22011-07-14 22:04:21 +00002250 "' can not set flags, but 's' suffix specified");
2251 }
2252
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002253 // Add the carry setting operand, if necessary.
2254 //
2255 // FIXME: It would be awesome if we could somehow invent a location such that
2256 // match errors on this operand would print a nice diagnostic about how the
2257 // 's' character in the mnemonic resulted in a CCOut operand.
Jim Grosbach33c16a22011-07-14 22:04:21 +00002258 if (CanAcceptCarrySet)
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002259 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2260 NameLoc));
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002261
2262 // Add the predication code operand, if necessary.
2263 if (CanAcceptPredicationCode) {
2264 Operands.push_back(ARMOperand::CreateCondCode(
2265 ARMCC::CondCodes(PredicationCode), NameLoc));
2266 } else {
2267 // This mnemonic can't ever accept a predication code, but the user wrote
2268 // one (or misspelled another mnemonic).
2269
2270 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002271 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002272
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002273 // Add the processor imod operand, if necessary.
2274 if (ProcessorIMod) {
2275 Operands.push_back(ARMOperand::CreateImm(
2276 MCConstantExpr::Create(ProcessorIMod, getContext()),
2277 NameLoc, NameLoc));
2278 } else {
2279 // This mnemonic can't ever accept a imod, but the user wrote
2280 // one (or misspelled another mnemonic).
2281
2282 // FIXME: Issue a nice error.
2283 }
2284
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002285 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002286 while (Next != StringRef::npos) {
2287 Start = Next;
2288 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002289 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002290
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002291 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002292 }
2293
2294 // Read the remaining operands.
2295 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002296 // Read the first operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002297 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002298 Parser.EatToEndOfStatement();
2299 return true;
2300 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002301
2302 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002303 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002304
2305 // Parse and remember the operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002306 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002307 Parser.EatToEndOfStatement();
2308 return true;
2309 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002310 }
2311 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002312
Chris Lattnercbf8a982010-09-11 16:18:25 +00002313 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2314 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002315 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002316 }
Bill Wendling146018f2010-11-06 21:42:12 +00002317
Chris Lattner34e53142010-09-08 05:10:46 +00002318 Parser.Lex(); // Consume the EndOfStatement
Jim Grosbachffa32252011-07-19 19:13:28 +00002319
2320
2321 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
2322 // another does not. Specifically, the MOVW instruction does not. So we
2323 // special case it here and remove the defaulted (non-setting) cc_out
2324 // operand if that's the instruction we're trying to match.
2325 //
2326 // We do this post-processing of the explicit operands rather than just
2327 // conditionally adding the cc_out in the first place because we need
2328 // to check the type of the parsed immediate operand.
2329 if (Mnemonic == "mov" && Operands.size() > 4 &&
2330 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
Jim Grosbach731f2092011-07-19 19:45:44 +00002331 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
2332 static_cast<ARMOperand*>(Operands[1])->getReg() == 0) {
Jim Grosbachffa32252011-07-19 19:13:28 +00002333 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
2334 Operands.erase(Operands.begin() + 1);
2335 delete Op;
2336 }
2337
Chris Lattner98986712010-01-14 22:21:20 +00002338 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002339}
2340
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002341bool ARMAsmParser::
2342MatchAndEmitInstruction(SMLoc IDLoc,
2343 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2344 MCStreamer &Out) {
2345 MCInst Inst;
2346 unsigned ErrorInfo;
Jim Grosbach5a187002011-07-19 18:32:48 +00002347 MatchResultTy MatchResult;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002348 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002349 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002350 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002351 Out.EmitInstruction(Inst);
2352 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002353 case Match_MissingFeature:
2354 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2355 return true;
2356 case Match_InvalidOperand: {
2357 SMLoc ErrorLoc = IDLoc;
2358 if (ErrorInfo != ~0U) {
2359 if (ErrorInfo >= Operands.size())
2360 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002361
Chris Lattnere73d4f82010-10-28 21:41:58 +00002362 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2363 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2364 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002365
Chris Lattnere73d4f82010-10-28 21:41:58 +00002366 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002367 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002368 case Match_MnemonicFail:
2369 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002370 case Match_ConversionFail:
2371 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002372 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002373
Eric Christopherc223e2b2010-10-29 09:26:59 +00002374 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002375 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002376}
2377
Kevin Enderby515d5092009-10-15 20:48:48 +00002378/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002379bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2380 StringRef IDVal = DirectiveID.getIdentifier();
2381 if (IDVal == ".word")
2382 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002383 else if (IDVal == ".thumb")
2384 return ParseDirectiveThumb(DirectiveID.getLoc());
2385 else if (IDVal == ".thumb_func")
2386 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2387 else if (IDVal == ".code")
2388 return ParseDirectiveCode(DirectiveID.getLoc());
2389 else if (IDVal == ".syntax")
2390 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002391 return true;
2392}
2393
2394/// ParseDirectiveWord
2395/// ::= .word [ expression (, expression)* ]
2396bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2397 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2398 for (;;) {
2399 const MCExpr *Value;
2400 if (getParser().ParseExpression(Value))
2401 return true;
2402
Chris Lattneraaec2052010-01-19 19:46:13 +00002403 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002404
2405 if (getLexer().is(AsmToken::EndOfStatement))
2406 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002407
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002408 // FIXME: Improve diagnostic.
2409 if (getLexer().isNot(AsmToken::Comma))
2410 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002411 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002412 }
2413 }
2414
Sean Callananb9a25b72010-01-19 20:27:46 +00002415 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002416 return false;
2417}
2418
Kevin Enderby515d5092009-10-15 20:48:48 +00002419/// ParseDirectiveThumb
2420/// ::= .thumb
2421bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2422 if (getLexer().isNot(AsmToken::EndOfStatement))
2423 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002424 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002425
2426 // TODO: set thumb mode
2427 // TODO: tell the MC streamer the mode
2428 // getParser().getStreamer().Emit???();
2429 return false;
2430}
2431
2432/// ParseDirectiveThumbFunc
2433/// ::= .thumbfunc symbol_name
2434bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002435 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2436 bool isMachO = MAI.hasSubsectionsViaSymbols();
2437 StringRef Name;
2438
2439 // Darwin asm has function name after .thumb_func direction
2440 // ELF doesn't
2441 if (isMachO) {
2442 const AsmToken &Tok = Parser.getTok();
2443 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2444 return Error(L, "unexpected token in .thumb_func directive");
2445 Name = Tok.getString();
2446 Parser.Lex(); // Consume the identifier token.
2447 }
2448
Kevin Enderby515d5092009-10-15 20:48:48 +00002449 if (getLexer().isNot(AsmToken::EndOfStatement))
2450 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002451 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002452
Rafael Espindola64695402011-05-16 16:17:21 +00002453 // FIXME: assuming function name will be the line following .thumb_func
2454 if (!isMachO) {
2455 Name = Parser.getTok().getString();
2456 }
2457
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002458 // Mark symbol as a thumb symbol.
2459 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2460 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002461 return false;
2462}
2463
2464/// ParseDirectiveSyntax
2465/// ::= .syntax unified | divided
2466bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002467 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002468 if (Tok.isNot(AsmToken::Identifier))
2469 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002470 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002471 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002472 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002473 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002474 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002475 else
2476 return Error(L, "unrecognized syntax mode in .syntax directive");
2477
2478 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002479 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002480 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002481
2482 // TODO tell the MC streamer the mode
2483 // getParser().getStreamer().Emit???();
2484 return false;
2485}
2486
2487/// ParseDirectiveCode
2488/// ::= .code 16 | 32
2489bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002490 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002491 if (Tok.isNot(AsmToken::Integer))
2492 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002493 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002494 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002495 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002496 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002497 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002498 else
2499 return Error(L, "invalid operand to .code directive");
2500
2501 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002502 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002503 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002504
Evan Cheng32869202011-07-08 22:36:29 +00002505 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002506 if (!isThumb())
2507 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002508 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002509 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002510 if (isThumb())
2511 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002512 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002513 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002514
Kevin Enderby515d5092009-10-15 20:48:48 +00002515 return false;
2516}
2517
Sean Callanan90b70972010-04-07 20:29:34 +00002518extern "C" void LLVMInitializeARMAsmLexer();
2519
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002520/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002521extern "C" void LLVMInitializeARMAsmParser() {
2522 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2523 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002524 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002525}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002526
Chris Lattner0692ee62010-09-06 19:11:01 +00002527#define GET_REGISTER_MATCHER
2528#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002529#include "ARMGenAsmMatcher.inc"