blob: ed5235ffbad73f41f470bea7243a812033cf5640 [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 Grosbachfff76ee2011-07-13 20:10:10 +0000421 bool isImm0_65535() 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 < 65536;
428 }
Jim Grosbachffa32252011-07-19 19:13:28 +0000429 bool isImm0_65535Expr() const {
430 if (Kind != Immediate)
431 return false;
432 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
433 // If it's not a constant expression, it'll generate a fixup and be
434 // handled later.
435 if (!CE) return true;
436 int64_t Value = CE->getValue();
437 return Value >= 0 && Value < 65536;
438 }
Jim Grosbachf6c05252011-07-21 17:23:04 +0000439 bool isPKHLSLImm() const {
440 if (Kind != Immediate)
441 return false;
442 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
443 if (!CE) return false;
444 int64_t Value = CE->getValue();
445 return Value >= 0 && Value < 32;
446 }
447 bool isPKHASRImm() 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 }
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000455 bool isARMSOImm() 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 ARM_AM::getSOImmVal(Value) != -1;
462 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000463 bool isT2SOImm() 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::getT2SOImmVal(Value) != -1;
470 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000471 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000472 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000473 bool isDPRRegList() const { return Kind == DPRRegisterList; }
474 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000475 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000476 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000477 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000478 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000479 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Owen Anderson92a20222011-07-21 18:54:16 +0000480 bool isShiftedImm() const { return Kind == ShiftedImmediate; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000481 bool isMemMode2() const {
482 if (getMemAddrMode() != ARMII::AddrMode2)
483 return false;
484
485 if (getMemOffsetIsReg())
486 return true;
487
488 if (getMemNegative() &&
489 !(getMemPostindexed() || getMemPreindexed()))
490 return false;
491
492 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
493 if (!CE) return false;
494 int64_t Value = CE->getValue();
495
496 // The offset must be in the range 0-4095 (imm12).
497 if (Value > 4095 || Value < -4095)
498 return false;
499
500 return true;
501 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000502 bool isMemMode3() const {
503 if (getMemAddrMode() != ARMII::AddrMode3)
504 return false;
505
506 if (getMemOffsetIsReg()) {
507 if (getMemOffsetRegShifted())
508 return false; // No shift with offset reg allowed
509 return true;
510 }
511
512 if (getMemNegative() &&
513 !(getMemPostindexed() || getMemPreindexed()))
514 return false;
515
516 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
517 if (!CE) return false;
518 int64_t Value = CE->getValue();
519
520 // The offset must be in the range 0-255 (imm8).
521 if (Value > 255 || Value < -255)
522 return false;
523
524 return true;
525 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000526 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000527 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
528 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000529 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000530
Daniel Dunbar4b462672011-01-18 05:55:27 +0000531 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000532 if (!CE) return false;
533
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000534 // The offset must be a multiple of 4 in the range 0-1020.
535 int64_t Value = CE->getValue();
536 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
537 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000538 bool isMemMode7() const {
539 if (!isMemory() ||
540 getMemPreindexed() ||
541 getMemPostindexed() ||
542 getMemOffsetIsReg() ||
543 getMemNegative() ||
544 getMemWriteback())
545 return false;
546
547 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
548 if (!CE) return false;
549
550 if (CE->getValue())
551 return false;
552
553 return true;
554 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000555 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000556 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000557 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000558 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000559 }
560 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000561 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000562 return false;
563
Daniel Dunbar4b462672011-01-18 05:55:27 +0000564 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000565 if (!CE) return false;
566
567 // The offset must be a multiple of 4 in the range 0-124.
568 uint64_t Value = CE->getValue();
569 return ((Value & 0x3) == 0 && Value <= 124);
570 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000571 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000572 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000573
574 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000575 // Add as immediates when possible. Null MCExpr = 0.
576 if (Expr == 0)
577 Inst.addOperand(MCOperand::CreateImm(0));
578 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000579 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
580 else
581 Inst.addOperand(MCOperand::CreateExpr(Expr));
582 }
583
Daniel Dunbar8462b302010-08-11 06:36:53 +0000584 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000585 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000586 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000587 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
588 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000589 }
590
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000591 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
592 assert(N == 1 && "Invalid number of operands!");
593 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
594 }
595
596 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
597 assert(N == 1 && "Invalid number of operands!");
598 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
599 }
600
Jim Grosbachd67641b2010-12-06 18:21:12 +0000601 void addCCOutOperands(MCInst &Inst, unsigned N) const {
602 assert(N == 1 && "Invalid number of operands!");
603 Inst.addOperand(MCOperand::CreateReg(getReg()));
604 }
605
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000606 void addRegOperands(MCInst &Inst, unsigned N) const {
607 assert(N == 1 && "Invalid number of operands!");
608 Inst.addOperand(MCOperand::CreateReg(getReg()));
609 }
610
Jim Grosbache8606dc2011-07-13 17:50:29 +0000611 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
612 assert(N == 3 && "Invalid number of operands!");
613 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
Jim Grosbache8606dc2011-07-13 17:50:29 +0000614 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
615 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
616 Inst.addOperand(MCOperand::CreateImm(
617 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
618 }
619
Owen Anderson92a20222011-07-21 18:54:16 +0000620 void addShiftedImmOperands(MCInst &Inst, unsigned N) const {
621 assert(N == 3 && "Invalid number of operands!");
622 assert(isShiftedImm() && "addShiftedImmOperands() on non ShiftedImm!");
623 Inst.addOperand(MCOperand::CreateReg(ShiftedImm.SrcReg));
624 if (ShiftedImm.ShiftTy == ARM_AM::rrx)
625 Inst.addOperand(MCOperand::CreateReg(ShiftedImm.SrcReg));
626 else
627 Inst.addOperand(MCOperand::CreateReg(0));
628 Inst.addOperand(MCOperand::CreateImm(
629 ARM_AM::getSORegOpc(ShiftedImm.ShiftTy, ShiftedImm.ShiftImm)));
630 }
631
632
Owen Anderson00828302011-03-18 22:50:18 +0000633 void addShifterOperands(MCInst &Inst, unsigned N) const {
634 assert(N == 1 && "Invalid number of operands!");
635 Inst.addOperand(MCOperand::CreateImm(
636 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
637 }
638
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000639 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000640 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000641 const SmallVectorImpl<unsigned> &RegList = getRegList();
642 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000643 I = RegList.begin(), E = RegList.end(); I != E; ++I)
644 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000645 }
646
Bill Wendling0f630752010-11-17 04:32:08 +0000647 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
648 addRegListOperands(Inst, N);
649 }
650
651 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
652 addRegListOperands(Inst, N);
653 }
654
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000655 void addImmOperands(MCInst &Inst, unsigned N) const {
656 assert(N == 1 && "Invalid number of operands!");
657 addExpr(Inst, getImm());
658 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000659
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000660 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
661 assert(N == 1 && "Invalid number of operands!");
662 addExpr(Inst, getImm());
663 }
664
Jim Grosbach83ab0702011-07-13 22:01:08 +0000665 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
666 assert(N == 1 && "Invalid number of operands!");
667 addExpr(Inst, getImm());
668 }
669
670 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
671 assert(N == 1 && "Invalid number of operands!");
672 addExpr(Inst, getImm());
673 }
674
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000675 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
676 assert(N == 1 && "Invalid number of operands!");
677 addExpr(Inst, getImm());
678 }
679
Jim Grosbachffa32252011-07-19 19:13:28 +0000680 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
681 assert(N == 1 && "Invalid number of operands!");
682 addExpr(Inst, getImm());
683 }
684
Jim Grosbachf6c05252011-07-21 17:23:04 +0000685 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
686 assert(N == 1 && "Invalid number of operands!");
687 addExpr(Inst, getImm());
688 }
689
690 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
691 assert(N == 1 && "Invalid number of operands!");
692 // An ASR value of 32 encodes as 0, so that's how we want to add it to
693 // the instruction as well.
694 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
695 int Val = CE->getValue();
696 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
697 }
698
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000699 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
700 assert(N == 1 && "Invalid number of operands!");
701 addExpr(Inst, getImm());
702 }
703
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000704 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
705 assert(N == 1 && "Invalid number of operands!");
706 addExpr(Inst, getImm());
707 }
708
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000709 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
710 assert(N == 1 && "Invalid number of operands!");
711 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
712 }
713
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000714 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
715 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
716 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
717
718 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000719 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000720 assert((CE || CE->getValue() == 0) &&
721 "No offset operand support in mode 7");
722 }
723
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000724 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
725 assert(isMemMode2() && "Invalid mode or number of operands!");
726 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
727 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
728
729 if (getMemOffsetIsReg()) {
730 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
731
732 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
733 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
734 int64_t ShiftAmount = 0;
735
736 if (getMemOffsetRegShifted()) {
737 ShOpc = getMemShiftType();
738 const MCConstantExpr *CE =
739 dyn_cast<MCConstantExpr>(getMemShiftAmount());
740 ShiftAmount = CE->getValue();
741 }
742
743 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
744 ShOpc, IdxMode)));
745 return;
746 }
747
748 // Create a operand placeholder to always yield the same number of operands.
749 Inst.addOperand(MCOperand::CreateReg(0));
750
751 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
752 // the difference?
753 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
754 assert(CE && "Non-constant mode 2 offset operand!");
755 int64_t Offset = CE->getValue();
756
757 if (Offset >= 0)
758 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
759 Offset, ARM_AM::no_shift, IdxMode)));
760 else
761 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
762 -Offset, ARM_AM::no_shift, IdxMode)));
763 }
764
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000765 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
766 assert(isMemMode3() && "Invalid mode or number of operands!");
767 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
768 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
769
770 if (getMemOffsetIsReg()) {
771 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
772
773 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
774 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
775 IdxMode)));
776 return;
777 }
778
779 // Create a operand placeholder to always yield the same number of operands.
780 Inst.addOperand(MCOperand::CreateReg(0));
781
782 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
783 // the difference?
784 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
785 assert(CE && "Non-constant mode 3 offset operand!");
786 int64_t Offset = CE->getValue();
787
788 if (Offset >= 0)
789 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
790 Offset, IdxMode)));
791 else
792 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
793 -Offset, IdxMode)));
794 }
795
Chris Lattner14b93852010-10-29 00:27:31 +0000796 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
797 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000798
Daniel Dunbar4b462672011-01-18 05:55:27 +0000799 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
800 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000801
Jim Grosbach80eb2332010-10-29 17:41:25 +0000802 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
803 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000804 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000805 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000806
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000807 // The MCInst offset operand doesn't include the low two bits (like
808 // the instruction encoding).
809 int64_t Offset = CE->getValue() / 4;
810 if (Offset >= 0)
811 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
812 Offset)));
813 else
814 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
815 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000816 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000817
Bill Wendlingf4caf692010-12-14 03:36:38 +0000818 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
819 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000820 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
821 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000822 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000823
Bill Wendlingf4caf692010-12-14 03:36:38 +0000824 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
825 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000826 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
827 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000828 assert(CE && "Non-constant mode offset operand!");
829 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000830 }
831
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000832 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
833 assert(N == 1 && "Invalid number of operands!");
834 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
835 }
836
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000837 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
838 assert(N == 1 && "Invalid number of operands!");
839 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
840 }
841
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000842 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000843
Chris Lattner3a697562010-10-28 17:20:03 +0000844 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
845 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000846 Op->CC.Val = CC;
847 Op->StartLoc = S;
848 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000849 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000850 }
851
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000852 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
853 ARMOperand *Op = new ARMOperand(CoprocNum);
854 Op->Cop.Val = CopVal;
855 Op->StartLoc = S;
856 Op->EndLoc = S;
857 return Op;
858 }
859
860 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
861 ARMOperand *Op = new ARMOperand(CoprocReg);
862 Op->Cop.Val = CopVal;
863 Op->StartLoc = S;
864 Op->EndLoc = S;
865 return Op;
866 }
867
Jim Grosbachd67641b2010-12-06 18:21:12 +0000868 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
869 ARMOperand *Op = new ARMOperand(CCOut);
870 Op->Reg.RegNum = RegNum;
871 Op->StartLoc = S;
872 Op->EndLoc = S;
873 return Op;
874 }
875
Chris Lattner3a697562010-10-28 17:20:03 +0000876 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
877 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000878 Op->Tok.Data = Str.data();
879 Op->Tok.Length = Str.size();
880 Op->StartLoc = S;
881 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000882 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000883 }
884
Bill Wendling50d0f582010-11-18 23:43:05 +0000885 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000886 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000887 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000888 Op->StartLoc = S;
889 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000890 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000891 }
892
Jim Grosbache8606dc2011-07-13 17:50:29 +0000893 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
894 unsigned SrcReg,
895 unsigned ShiftReg,
896 unsigned ShiftImm,
897 SMLoc S, SMLoc E) {
898 ARMOperand *Op = new ARMOperand(ShiftedRegister);
899 Op->ShiftedReg.ShiftTy = ShTy;
900 Op->ShiftedReg.SrcReg = SrcReg;
901 Op->ShiftedReg.ShiftReg = ShiftReg;
902 Op->ShiftedReg.ShiftImm = ShiftImm;
903 Op->StartLoc = S;
904 Op->EndLoc = E;
905 return Op;
906 }
907
Owen Anderson92a20222011-07-21 18:54:16 +0000908 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
909 unsigned SrcReg,
910 unsigned ShiftImm,
911 SMLoc S, SMLoc E) {
912 ARMOperand *Op = new ARMOperand(ShiftedImmediate);
913 Op->ShiftedImm.ShiftTy = ShTy;
914 Op->ShiftedImm.SrcReg = SrcReg;
915 Op->ShiftedImm.ShiftImm = ShiftImm;
916 Op->StartLoc = S;
917 Op->EndLoc = E;
918 return Op;
919 }
920
Owen Anderson00828302011-03-18 22:50:18 +0000921 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
922 SMLoc S, SMLoc E) {
923 ARMOperand *Op = new ARMOperand(Shifter);
924 Op->Shift.ShiftTy = ShTy;
925 Op->StartLoc = S;
926 Op->EndLoc = E;
927 return Op;
928 }
929
Bill Wendling7729e062010-11-09 22:44:22 +0000930 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000931 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000932 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000933 KindTy Kind = RegisterList;
934
935 if (ARM::DPRRegClass.contains(Regs.front().first))
936 Kind = DPRRegisterList;
937 else if (ARM::SPRRegClass.contains(Regs.front().first))
938 Kind = SPRRegisterList;
939
940 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000941 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000942 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000943 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000944 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000945 Op->StartLoc = StartLoc;
946 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000947 return Op;
948 }
949
Chris Lattner3a697562010-10-28 17:20:03 +0000950 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
951 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000952 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000953 Op->StartLoc = S;
954 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000955 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000956 }
957
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000958 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
959 bool OffsetIsReg, const MCExpr *Offset,
960 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000961 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000962 const MCExpr *ShiftAmount, bool Preindexed,
963 bool Postindexed, bool Negative, bool Writeback,
964 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000965 assert((OffsetRegNum == -1 || OffsetIsReg) &&
966 "OffsetRegNum must imply OffsetIsReg!");
967 assert((!OffsetRegShifted || OffsetIsReg) &&
968 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000969 assert((Offset || OffsetIsReg) &&
970 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000971 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
972 "Cannot have shift amount without shifted register offset!");
973 assert((!Offset || !OffsetIsReg) &&
974 "Cannot have expression offset and register offset!");
975
Chris Lattner3a697562010-10-28 17:20:03 +0000976 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000977 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000978 Op->Mem.BaseRegNum = BaseRegNum;
979 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000980 if (OffsetIsReg)
981 Op->Mem.Offset.RegNum = OffsetRegNum;
982 else
983 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000984 Op->Mem.OffsetRegShifted = OffsetRegShifted;
985 Op->Mem.ShiftType = ShiftType;
986 Op->Mem.ShiftAmount = ShiftAmount;
987 Op->Mem.Preindexed = Preindexed;
988 Op->Mem.Postindexed = Postindexed;
989 Op->Mem.Negative = Negative;
990 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000991
Sean Callanan76264762010-04-02 22:27:05 +0000992 Op->StartLoc = S;
993 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000994 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000995 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000996
997 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
998 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
999 Op->MBOpt.Val = Opt;
1000 Op->StartLoc = S;
1001 Op->EndLoc = S;
1002 return Op;
1003 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001004
1005 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1006 ARMOperand *Op = new ARMOperand(ProcIFlags);
1007 Op->IFlags.Val = IFlags;
1008 Op->StartLoc = S;
1009 Op->EndLoc = S;
1010 return Op;
1011 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001012
1013 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1014 ARMOperand *Op = new ARMOperand(MSRMask);
1015 Op->MMask.Val = MMask;
1016 Op->StartLoc = S;
1017 Op->EndLoc = S;
1018 return Op;
1019 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001020};
1021
1022} // end anonymous namespace.
1023
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001024void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001025 switch (Kind) {
1026 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +00001027 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001028 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +00001029 case CCOut:
1030 OS << "<ccout " << getReg() << ">";
1031 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001032 case CoprocNum:
1033 OS << "<coprocessor number: " << getCoproc() << ">";
1034 break;
1035 case CoprocReg:
1036 OS << "<coprocessor register: " << getCoproc() << ">";
1037 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001038 case MSRMask:
1039 OS << "<mask: " << getMSRMask() << ">";
1040 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001041 case Immediate:
1042 getImm()->print(OS);
1043 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001044 case MemBarrierOpt:
1045 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1046 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001047 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001048 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001049 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
1050 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001051 if (getMemOffsetIsReg()) {
1052 OS << " offset:<register " << getMemOffsetRegNum();
1053 if (getMemOffsetRegShifted()) {
1054 OS << " offset-shift-type:" << getMemShiftType();
1055 OS << " offset-shift-amount:" << *getMemShiftAmount();
1056 }
1057 } else {
1058 OS << " offset:" << *getMemOffset();
1059 }
1060 if (getMemOffsetIsReg())
1061 OS << " (offset-is-reg)";
1062 if (getMemPreindexed())
1063 OS << " (pre-indexed)";
1064 if (getMemPostindexed())
1065 OS << " (post-indexed)";
1066 if (getMemNegative())
1067 OS << " (negative)";
1068 if (getMemWriteback())
1069 OS << " (writeback)";
1070 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001071 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001072 case ProcIFlags: {
1073 OS << "<ARM_PROC::";
1074 unsigned IFlags = getProcIFlags();
1075 for (int i=2; i >= 0; --i)
1076 if (IFlags & (1 << i))
1077 OS << ARM_PROC::IFlagsToString(1 << i);
1078 OS << ">";
1079 break;
1080 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001081 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +00001082 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001083 break;
Owen Anderson00828302011-03-18 22:50:18 +00001084 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +00001085 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
1086 break;
1087 case ShiftedRegister:
Owen Anderson92a20222011-07-21 18:54:16 +00001088 OS << "<so_reg_reg "
Jim Grosbache8606dc2011-07-13 17:50:29 +00001089 << ShiftedReg.SrcReg
1090 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
1091 << ", " << ShiftedReg.ShiftReg << ", "
1092 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
1093 << ">";
Owen Anderson00828302011-03-18 22:50:18 +00001094 break;
Owen Anderson92a20222011-07-21 18:54:16 +00001095 case ShiftedImmediate:
1096 OS << "<so_reg_imm "
1097 << ShiftedImm.SrcReg
1098 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedImm.ShiftImm))
1099 << ", " << ARM_AM::getSORegOffset(ShiftedImm.ShiftImm)
1100 << ">";
1101 break;
Bill Wendling0f630752010-11-17 04:32:08 +00001102 case RegisterList:
1103 case DPRRegisterList:
1104 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00001105 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001106
Bill Wendling5fa22a12010-11-09 23:28:44 +00001107 const SmallVectorImpl<unsigned> &RegList = getRegList();
1108 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001109 I = RegList.begin(), E = RegList.end(); I != E; ) {
1110 OS << *I;
1111 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001112 }
1113
1114 OS << ">";
1115 break;
1116 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001117 case Token:
1118 OS << "'" << getToken() << "'";
1119 break;
1120 }
1121}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001122
1123/// @name Auto-generated Match Functions
1124/// {
1125
1126static unsigned MatchRegisterName(StringRef Name);
1127
1128/// }
1129
Bob Wilson69df7232011-02-03 21:46:10 +00001130bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1131 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +00001132 RegNo = TryParseRegister();
1133
1134 return (RegNo == (unsigned)-1);
1135}
1136
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001137/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00001138/// and if it is a register name the token is eaten and the register number is
1139/// returned. Otherwise return -1.
1140///
1141int ARMAsmParser::TryParseRegister() {
1142 const AsmToken &Tok = Parser.getTok();
1143 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001144
Chris Lattnere5658fa2010-10-30 04:09:10 +00001145 // FIXME: Validate register for the current architecture; we have to do
1146 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001147 std::string upperCase = Tok.getString().str();
1148 std::string lowerCase = LowercaseString(upperCase);
1149 unsigned RegNum = MatchRegisterName(lowerCase);
1150 if (!RegNum) {
1151 RegNum = StringSwitch<unsigned>(lowerCase)
1152 .Case("r13", ARM::SP)
1153 .Case("r14", ARM::LR)
1154 .Case("r15", ARM::PC)
1155 .Case("ip", ARM::R12)
1156 .Default(0);
1157 }
1158 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001159
Chris Lattnere5658fa2010-10-30 04:09:10 +00001160 Parser.Lex(); // Eat identifier token.
1161 return RegNum;
1162}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001163
Jim Grosbach19906722011-07-13 18:49:30 +00001164// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1165// If a recoverable error occurs, return 1. If an irrecoverable error
1166// occurs, return -1. An irrecoverable error is one where tokens have been
1167// consumed in the process of trying to parse the shifter (i.e., when it is
1168// indeed a shifter operand, but malformed).
1169int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001170 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1171 SMLoc S = Parser.getTok().getLoc();
1172 const AsmToken &Tok = Parser.getTok();
1173 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1174
1175 std::string upperCase = Tok.getString().str();
1176 std::string lowerCase = LowercaseString(upperCase);
1177 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1178 .Case("lsl", ARM_AM::lsl)
1179 .Case("lsr", ARM_AM::lsr)
1180 .Case("asr", ARM_AM::asr)
1181 .Case("ror", ARM_AM::ror)
1182 .Case("rrx", ARM_AM::rrx)
1183 .Default(ARM_AM::no_shift);
1184
1185 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001186 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001187
Jim Grosbache8606dc2011-07-13 17:50:29 +00001188 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001189
Jim Grosbache8606dc2011-07-13 17:50:29 +00001190 // The source register for the shift has already been added to the
1191 // operand list, so we need to pop it off and combine it into the shifted
1192 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00001193 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00001194 if (!PrevOp->isReg())
1195 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1196 int SrcReg = PrevOp->getReg();
1197 int64_t Imm = 0;
1198 int ShiftReg = 0;
1199 if (ShiftTy == ARM_AM::rrx) {
1200 // RRX Doesn't have an explicit shift amount. The encoder expects
1201 // the shift register to be the same as the source register. Seems odd,
1202 // but OK.
1203 ShiftReg = SrcReg;
1204 } else {
1205 // Figure out if this is shifted by a constant or a register (for non-RRX).
1206 if (Parser.getTok().is(AsmToken::Hash)) {
1207 Parser.Lex(); // Eat hash.
1208 SMLoc ImmLoc = Parser.getTok().getLoc();
1209 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001210 if (getParser().ParseExpression(ShiftExpr)) {
1211 Error(ImmLoc, "invalid immediate shift value");
1212 return -1;
1213 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001214 // The expression must be evaluatable as an immediate.
1215 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001216 if (!CE) {
1217 Error(ImmLoc, "invalid immediate shift value");
1218 return -1;
1219 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001220 // Range check the immediate.
1221 // lsl, ror: 0 <= imm <= 31
1222 // lsr, asr: 0 <= imm <= 32
1223 Imm = CE->getValue();
1224 if (Imm < 0 ||
1225 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1226 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001227 Error(ImmLoc, "immediate shift value out of range");
1228 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001229 }
1230 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1231 ShiftReg = TryParseRegister();
1232 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001233 if (ShiftReg == -1) {
1234 Error (L, "expected immediate or register in shift operand");
1235 return -1;
1236 }
1237 } else {
1238 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001239 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001240 return -1;
1241 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001242 }
1243
Owen Anderson92a20222011-07-21 18:54:16 +00001244 if (ShiftReg && ShiftTy != ARM_AM::rrx)
1245 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
Jim Grosbache8606dc2011-07-13 17:50:29 +00001246 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001247 S, Parser.getTok().getLoc()));
Owen Anderson92a20222011-07-21 18:54:16 +00001248 else
1249 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
1250 S, Parser.getTok().getLoc()));
Owen Anderson00828302011-03-18 22:50:18 +00001251
Jim Grosbach19906722011-07-13 18:49:30 +00001252 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001253}
1254
1255
Bill Wendling50d0f582010-11-18 23:43:05 +00001256/// Try to parse a register name. The token must be an Identifier when called.
1257/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1258/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001259///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001260/// TODO this is likely to change to allow different register types and or to
1261/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001262bool ARMAsmParser::
1263TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001264 SMLoc S = Parser.getTok().getLoc();
1265 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001266 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001267 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001268
Bill Wendling50d0f582010-11-18 23:43:05 +00001269 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001270
Chris Lattnere5658fa2010-10-30 04:09:10 +00001271 const AsmToken &ExclaimTok = Parser.getTok();
1272 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001273 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1274 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001275 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001276 }
1277
Bill Wendling50d0f582010-11-18 23:43:05 +00001278 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001279}
1280
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001281/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1282/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1283/// "c5", ...
1284static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001285 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1286 // but efficient.
1287 switch (Name.size()) {
1288 default: break;
1289 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001290 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001291 return -1;
1292 switch (Name[1]) {
1293 default: return -1;
1294 case '0': return 0;
1295 case '1': return 1;
1296 case '2': return 2;
1297 case '3': return 3;
1298 case '4': return 4;
1299 case '5': return 5;
1300 case '6': return 6;
1301 case '7': return 7;
1302 case '8': return 8;
1303 case '9': return 9;
1304 }
1305 break;
1306 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001307 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001308 return -1;
1309 switch (Name[2]) {
1310 default: return -1;
1311 case '0': return 10;
1312 case '1': return 11;
1313 case '2': return 12;
1314 case '3': return 13;
1315 case '4': return 14;
1316 case '5': return 15;
1317 }
1318 break;
1319 }
1320
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001321 return -1;
1322}
1323
Jim Grosbachf922c472011-02-12 01:34:40 +00001324/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001325/// token must be an Identifier when called, and if it is a coprocessor
1326/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001327ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1328tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001329 SMLoc S = Parser.getTok().getLoc();
1330 const AsmToken &Tok = Parser.getTok();
1331 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1332
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001333 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001334 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001335 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001336
1337 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001338 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001339 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001340}
1341
Jim Grosbachf922c472011-02-12 01:34:40 +00001342/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001343/// token must be an Identifier when called, and if it is a coprocessor
1344/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001345ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1346tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001347 SMLoc S = Parser.getTok().getLoc();
1348 const AsmToken &Tok = Parser.getTok();
1349 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1350
1351 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1352 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001353 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001354
1355 Parser.Lex(); // Eat identifier token.
1356 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001357 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001358}
1359
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001360/// Parse a register list, return it if successful else return null. The first
1361/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001362bool ARMAsmParser::
1363ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001364 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001365 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001366 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001367
Bill Wendling7729e062010-11-09 22:44:22 +00001368 // Read the rest of the registers in the list.
1369 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001370 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001371
Bill Wendling7729e062010-11-09 22:44:22 +00001372 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001373 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001374 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001375
Sean Callanan18b83232010-01-19 21:44:56 +00001376 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001377 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001378 if (RegTok.isNot(AsmToken::Identifier)) {
1379 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001380 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001381 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001382
Bill Wendling1d6a2652010-11-06 10:40:24 +00001383 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001384 if (RegNum == -1) {
1385 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001386 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001387 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001388
Bill Wendlinge7176102010-11-06 22:36:58 +00001389 if (IsRange) {
1390 int Reg = PrevRegNum;
1391 do {
1392 ++Reg;
1393 Registers.push_back(std::make_pair(Reg, RegLoc));
1394 } while (Reg != RegNum);
1395 } else {
1396 Registers.push_back(std::make_pair(RegNum, RegLoc));
1397 }
1398
1399 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001400 } while (Parser.getTok().is(AsmToken::Comma) ||
1401 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001402
1403 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001404 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001405 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1406 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001407 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001408 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001409
Bill Wendlinge7176102010-11-06 22:36:58 +00001410 SMLoc E = RCurlyTok.getLoc();
1411 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001412
Bill Wendlinge7176102010-11-06 22:36:58 +00001413 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001414 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001415 RI = Registers.begin(), RE = Registers.end();
1416
Bill Wendling7caebff2011-01-12 21:20:59 +00001417 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001418 bool EmittedWarning = false;
1419
Bill Wendling7caebff2011-01-12 21:20:59 +00001420 DenseMap<unsigned, bool> RegMap;
1421 RegMap[HighRegNum] = true;
1422
Bill Wendlinge7176102010-11-06 22:36:58 +00001423 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001424 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001425 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001426
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001427 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001428 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001429 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001430 }
1431
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001432 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001433 Warning(RegInfo.second,
1434 "register not in ascending order in register list");
1435
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001436 RegMap[Reg] = true;
1437 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001438 }
1439
Bill Wendling50d0f582010-11-18 23:43:05 +00001440 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1441 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001442}
1443
Jim Grosbachf922c472011-02-12 01:34:40 +00001444/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1445ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1446tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001447 SMLoc S = Parser.getTok().getLoc();
1448 const AsmToken &Tok = Parser.getTok();
1449 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1450 StringRef OptStr = Tok.getString();
1451
1452 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1453 .Case("sy", ARM_MB::SY)
1454 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001455 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001456 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001457 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001458 .Case("ishst", ARM_MB::ISHST)
1459 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001460 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001461 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001462 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001463 .Case("osh", ARM_MB::OSH)
1464 .Case("oshst", ARM_MB::OSHST)
1465 .Default(~0U);
1466
1467 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001468 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001469
1470 Parser.Lex(); // Eat identifier token.
1471 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001472 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001473}
1474
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001475/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001476ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1477tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1478 SMLoc S = Parser.getTok().getLoc();
1479 const AsmToken &Tok = Parser.getTok();
1480 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1481 StringRef IFlagsStr = Tok.getString();
1482
1483 unsigned IFlags = 0;
1484 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1485 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1486 .Case("a", ARM_PROC::A)
1487 .Case("i", ARM_PROC::I)
1488 .Case("f", ARM_PROC::F)
1489 .Default(~0U);
1490
1491 // If some specific iflag is already set, it means that some letter is
1492 // present more than once, this is not acceptable.
1493 if (Flag == ~0U || (IFlags & Flag))
1494 return MatchOperand_NoMatch;
1495
1496 IFlags |= Flag;
1497 }
1498
1499 Parser.Lex(); // Eat identifier token.
1500 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1501 return MatchOperand_Success;
1502}
1503
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001504/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1505ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1506tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1507 SMLoc S = Parser.getTok().getLoc();
1508 const AsmToken &Tok = Parser.getTok();
1509 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1510 StringRef Mask = Tok.getString();
1511
1512 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1513 size_t Start = 0, Next = Mask.find('_');
1514 StringRef Flags = "";
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00001515 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001516 if (Next != StringRef::npos)
1517 Flags = Mask.slice(Next+1, Mask.size());
1518
1519 // FlagsVal contains the complete mask:
1520 // 3-0: Mask
1521 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1522 unsigned FlagsVal = 0;
1523
1524 if (SpecReg == "apsr") {
1525 FlagsVal = StringSwitch<unsigned>(Flags)
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00001526 .Case("nzcvq", 0x8) // same as CPSR_f
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001527 .Case("g", 0x4) // same as CPSR_s
1528 .Case("nzcvqg", 0xc) // same as CPSR_fs
1529 .Default(~0U);
1530
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001531 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001532 if (!Flags.empty())
1533 return MatchOperand_NoMatch;
1534 else
1535 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001536 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001537 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001538 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1539 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001540 for (int i = 0, e = Flags.size(); i != e; ++i) {
1541 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1542 .Case("c", 1)
1543 .Case("x", 2)
1544 .Case("s", 4)
1545 .Case("f", 8)
1546 .Default(~0U);
1547
1548 // If some specific flag is already set, it means that some letter is
1549 // present more than once, this is not acceptable.
1550 if (FlagsVal == ~0U || (FlagsVal & Flag))
1551 return MatchOperand_NoMatch;
1552 FlagsVal |= Flag;
1553 }
1554 } else // No match for special register.
1555 return MatchOperand_NoMatch;
1556
1557 // Special register without flags are equivalent to "fc" flags.
1558 if (!FlagsVal)
1559 FlagsVal = 0x9;
1560
1561 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1562 if (SpecReg == "spsr")
1563 FlagsVal |= 16;
1564
1565 Parser.Lex(); // Eat identifier token.
1566 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1567 return MatchOperand_Success;
1568}
1569
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001570/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1571ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1572tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001573 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001574
1575 if (ParseMemory(Operands, ARMII::AddrMode2))
1576 return MatchOperand_NoMatch;
1577
1578 return MatchOperand_Success;
1579}
1580
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001581/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1582ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1583tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1584 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1585
1586 if (ParseMemory(Operands, ARMII::AddrMode3))
1587 return MatchOperand_NoMatch;
1588
1589 return MatchOperand_Success;
1590}
1591
Jim Grosbachf6c05252011-07-21 17:23:04 +00001592ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1593parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
1594 int Low, int High) {
1595 const AsmToken &Tok = Parser.getTok();
1596 if (Tok.isNot(AsmToken::Identifier)) {
1597 Error(Parser.getTok().getLoc(), Op + " operand expected.");
1598 return MatchOperand_ParseFail;
1599 }
1600 StringRef ShiftName = Tok.getString();
1601 std::string LowerOp = LowercaseString(Op);
1602 std::string UpperOp = UppercaseString(Op);
1603 if (ShiftName != LowerOp && ShiftName != UpperOp) {
1604 Error(Parser.getTok().getLoc(), Op + " operand expected.");
1605 return MatchOperand_ParseFail;
1606 }
1607 Parser.Lex(); // Eat shift type token.
1608
1609 // There must be a '#' and a shift amount.
1610 if (Parser.getTok().isNot(AsmToken::Hash)) {
1611 Error(Parser.getTok().getLoc(), "'#' expected");
1612 return MatchOperand_ParseFail;
1613 }
1614 Parser.Lex(); // Eat hash token.
1615
1616 const MCExpr *ShiftAmount;
1617 SMLoc Loc = Parser.getTok().getLoc();
1618 if (getParser().ParseExpression(ShiftAmount)) {
1619 Error(Loc, "illegal expression");
1620 return MatchOperand_ParseFail;
1621 }
1622 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
1623 if (!CE) {
1624 Error(Loc, "constant expression expected");
1625 return MatchOperand_ParseFail;
1626 }
1627 int Val = CE->getValue();
1628 if (Val < Low || Val > High) {
1629 Error(Loc, "immediate value out of range");
1630 return MatchOperand_ParseFail;
1631 }
1632
1633 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
1634
1635 return MatchOperand_Success;
1636}
1637
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001638/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1639/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1640/// when they refer multiple MIOperands inside a single one.
1641bool ARMAsmParser::
1642CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1643 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1644 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1645
1646 // Create a writeback register dummy placeholder.
1647 Inst.addOperand(MCOperand::CreateImm(0));
1648
1649 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1650 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1651 return true;
1652}
1653
1654/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1655/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1656/// when they refer multiple MIOperands inside a single one.
1657bool ARMAsmParser::
1658CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1659 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1660 // Create a writeback register dummy placeholder.
1661 Inst.addOperand(MCOperand::CreateImm(0));
1662 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1663 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1664 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1665 return true;
1666}
1667
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001668/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1669/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1670/// when they refer multiple MIOperands inside a single one.
1671bool ARMAsmParser::
1672CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1673 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1674 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1675
1676 // Create a writeback register dummy placeholder.
1677 Inst.addOperand(MCOperand::CreateImm(0));
1678
1679 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1680 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1681 return true;
1682}
1683
1684/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1685/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1686/// when they refer multiple MIOperands inside a single one.
1687bool ARMAsmParser::
1688CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1689 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1690 // Create a writeback register dummy placeholder.
1691 Inst.addOperand(MCOperand::CreateImm(0));
1692 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1693 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1694 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1695 return true;
1696}
1697
Bill Wendlinge7176102010-11-06 22:36:58 +00001698/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001699/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001700///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001701/// TODO Only preindexing and postindexing addressing are started, unindexed
1702/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001703bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001704ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1705 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001706 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001707 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001708 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001709 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001710 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001711
Sean Callanan18b83232010-01-19 21:44:56 +00001712 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001713 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1714 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001715 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001716 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001717 int BaseRegNum = TryParseRegister();
1718 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001719 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001720 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001721 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001722
Daniel Dunbar05710932011-01-18 05:34:17 +00001723 // The next token must either be a comma or a closing bracket.
1724 const AsmToken &Tok = Parser.getTok();
1725 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1726 return true;
1727
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001728 bool Preindexed = false;
1729 bool Postindexed = false;
1730 bool OffsetIsReg = false;
1731 bool Negative = false;
1732 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001733 ARMOperand *WBOp = 0;
1734 int OffsetRegNum = -1;
1735 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001736 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001737 const MCExpr *ShiftAmount = 0;
1738 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001739
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001740 // First look for preindexed address forms, that is after the "[Rn" we now
1741 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001742 if (Tok.is(AsmToken::Comma)) {
1743 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001744 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001745
Chris Lattner550276e2010-10-28 20:52:15 +00001746 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1747 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001748 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001749 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001750 if (RBracTok.isNot(AsmToken::RBrac)) {
1751 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001752 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001753 }
Sean Callanan76264762010-04-02 22:27:05 +00001754 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001755 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001756
Sean Callanan18b83232010-01-19 21:44:56 +00001757 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001758 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001759 // None of addrmode3 instruction uses "!"
1760 if (AddrMode == ARMII::AddrMode3)
1761 return true;
1762
Bill Wendling50d0f582010-11-18 23:43:05 +00001763 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1764 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001765 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001766 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001767 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1768 if (AddrMode == ARMII::AddrMode2)
1769 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001770 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001771 } else {
1772 // The "[Rn" we have so far was not followed by a comma.
1773
Jim Grosbach80eb2332010-10-29 17:41:25 +00001774 // If there's anything other than the right brace, this is a post indexing
1775 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001776 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001777 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001778
Sean Callanan18b83232010-01-19 21:44:56 +00001779 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001780
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001781 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001782 Postindexed = true;
1783 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001784
Chris Lattner550276e2010-10-28 20:52:15 +00001785 if (NextTok.isNot(AsmToken::Comma)) {
1786 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001787 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001788 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001789
Sean Callananb9a25b72010-01-19 20:27:46 +00001790 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001791
Chris Lattner550276e2010-10-28 20:52:15 +00001792 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001793 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001794 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001795 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001796 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001797 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001798
1799 // Force Offset to exist if used.
1800 if (!OffsetIsReg) {
1801 if (!Offset)
1802 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001803 } else {
1804 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1805 Error(E, "shift amount not supported");
1806 return true;
1807 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001808 }
1809
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001810 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1811 Offset, OffsetRegNum, OffsetRegShifted,
1812 ShiftType, ShiftAmount, Preindexed,
1813 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001814 if (WBOp)
1815 Operands.push_back(WBOp);
1816
1817 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001818}
1819
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001820/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1821/// we will parse the following (were +/- means that a plus or minus is
1822/// optional):
1823/// +/-Rm
1824/// +/-Rm, shift
1825/// #offset
1826/// we return false on success or an error otherwise.
1827bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001828 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001829 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001830 const MCExpr *&ShiftAmount,
1831 const MCExpr *&Offset,
1832 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001833 int &OffsetRegNum,
1834 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001835 Negative = false;
1836 OffsetRegShifted = false;
1837 OffsetIsReg = false;
1838 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001839 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001840 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001841 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001842 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001843 else if (NextTok.is(AsmToken::Minus)) {
1844 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001845 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001846 }
1847 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001848 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001849 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001850 SMLoc CurLoc = OffsetRegTok.getLoc();
1851 OffsetRegNum = TryParseRegister();
1852 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001853 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001854 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001855 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001856 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001857
Bill Wendling12f40e92010-11-06 10:51:53 +00001858 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001859 if (OffsetRegNum != -1) {
1860 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001861 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001862 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001863 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001864
Sean Callanan18b83232010-01-19 21:44:56 +00001865 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001866 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001867 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001868 OffsetRegShifted = true;
1869 }
1870 }
1871 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1872 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001873 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001874 if (HashTok.isNot(AsmToken::Hash))
1875 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001876
Sean Callananb9a25b72010-01-19 20:27:46 +00001877 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001878
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001879 if (getParser().ParseExpression(Offset))
1880 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001881 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001882 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001883 return false;
1884}
1885
1886/// ParseShift as one of these two:
1887/// ( lsl | lsr | asr | ror ) , # shift_amount
1888/// rrx
1889/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001890bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1891 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001892 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001893 if (Tok.isNot(AsmToken::Identifier))
1894 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001895 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001896 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001897 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001898 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001899 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001900 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001901 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001902 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001903 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001904 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001905 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001906 else
1907 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001908 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001909
1910 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001911 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001912 return false;
1913
1914 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001915 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001916 if (HashTok.isNot(AsmToken::Hash))
1917 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001918 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001919
1920 if (getParser().ParseExpression(ShiftAmount))
1921 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001922
1923 return false;
1924}
1925
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001926/// Parse a arm instruction operand. For now this parses the operand regardless
1927/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001928bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001929 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001930 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001931
1932 // Check if the current operand has a custom associated parser, if so, try to
1933 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001934 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1935 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001936 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001937 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1938 // there was a match, but an error occurred, in which case, just return that
1939 // the operand parsing failed.
1940 if (ResTy == MatchOperand_ParseFail)
1941 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001942
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001943 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001944 default:
1945 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001946 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001947 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001948 if (!TryParseRegisterWithWriteBack(Operands))
1949 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001950 int Res = TryParseShiftRegister(Operands);
1951 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001952 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001953 else if (Res == -1) // irrecoverable error
1954 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001955
1956 // Fall though for the Identifier case that is not a register or a
1957 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00001958 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00001959 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1960 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001961 // This was not a register so parse other operands that start with an
1962 // identifier (like labels) as expressions and create them as immediates.
1963 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001964 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001965 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001966 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001967 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001968 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1969 return false;
1970 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001971 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001972 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001973 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001974 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001975 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001976 // #42 -> immediate.
1977 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001978 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001979 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001980 const MCExpr *ImmVal;
1981 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001982 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001983 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001984 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1985 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001986 case AsmToken::Colon: {
1987 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001988 // FIXME: Check it's an expression prefix,
1989 // e.g. (FOO - :lower16:BAR) isn't legal.
1990 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001991 if (ParsePrefix(RefKind))
1992 return true;
1993
Evan Cheng75972122011-01-13 07:58:56 +00001994 const MCExpr *SubExprVal;
1995 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001996 return true;
1997
Evan Cheng75972122011-01-13 07:58:56 +00001998 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1999 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00002000 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00002001 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00002002 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002003 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00002004 }
2005}
2006
Evan Cheng75972122011-01-13 07:58:56 +00002007// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
2008// :lower16: and :upper16:.
2009bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
2010 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002011
2012 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00002013 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00002014 Parser.Lex(); // Eat ':'
2015
2016 if (getLexer().isNot(AsmToken::Identifier)) {
2017 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
2018 return true;
2019 }
2020
2021 StringRef IDVal = Parser.getTok().getIdentifier();
2022 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00002023 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002024 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00002025 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00002026 } else {
2027 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
2028 return true;
2029 }
2030 Parser.Lex();
2031
2032 if (getLexer().isNot(AsmToken::Colon)) {
2033 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
2034 return true;
2035 }
2036 Parser.Lex(); // Eat the last ':'
2037 return false;
2038}
2039
2040const MCExpr *
2041ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
2042 MCSymbolRefExpr::VariantKind Variant) {
2043 // Recurse over the given expression, rebuilding it to apply the given variant
2044 // to the leftmost symbol.
2045 if (Variant == MCSymbolRefExpr::VK_None)
2046 return E;
2047
2048 switch (E->getKind()) {
2049 case MCExpr::Target:
2050 llvm_unreachable("Can't handle target expr yet");
2051 case MCExpr::Constant:
2052 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
2053
2054 case MCExpr::SymbolRef: {
2055 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
2056
2057 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
2058 return 0;
2059
2060 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
2061 }
2062
2063 case MCExpr::Unary:
2064 llvm_unreachable("Can't handle unary expressions yet");
2065
2066 case MCExpr::Binary: {
2067 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
2068 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
2069 const MCExpr *RHS = BE->getRHS();
2070 if (!LHS)
2071 return 0;
2072
2073 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
2074 }
2075 }
2076
2077 assert(0 && "Invalid expression kind!");
2078 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002079}
2080
Daniel Dunbar352e1482011-01-11 15:59:50 +00002081/// \brief Given a mnemonic, split out possible predication code and carry
2082/// setting letters to form a canonical mnemonic and flags.
2083//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002084// FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00002085StringRef ARMAsmParser::SplitMnemonic(StringRef Mnemonic,
2086 unsigned &PredicationCode,
2087 bool &CarrySetting,
2088 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00002089 PredicationCode = ARMCC::AL;
2090 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002091 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002092
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002093 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00002094 //
2095 // FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00002096 if ((Mnemonic == "movs" && isThumb()) ||
2097 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
2098 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
2099 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
2100 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
2101 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
2102 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
2103 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
Daniel Dunbar352e1482011-01-11 15:59:50 +00002104 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00002105
Jim Grosbach3f00e312011-07-11 17:09:57 +00002106 // First, split out any predication code. Ignore mnemonics we know aren't
2107 // predicated but do have a carry-set and so weren't caught above.
Jim Grosbachab40f4b2011-07-20 18:20:31 +00002108 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
2109 Mnemonic != "muls") {
Jim Grosbach3f00e312011-07-11 17:09:57 +00002110 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
2111 .Case("eq", ARMCC::EQ)
2112 .Case("ne", ARMCC::NE)
2113 .Case("hs", ARMCC::HS)
2114 .Case("cs", ARMCC::HS)
2115 .Case("lo", ARMCC::LO)
2116 .Case("cc", ARMCC::LO)
2117 .Case("mi", ARMCC::MI)
2118 .Case("pl", ARMCC::PL)
2119 .Case("vs", ARMCC::VS)
2120 .Case("vc", ARMCC::VC)
2121 .Case("hi", ARMCC::HI)
2122 .Case("ls", ARMCC::LS)
2123 .Case("ge", ARMCC::GE)
2124 .Case("lt", ARMCC::LT)
2125 .Case("gt", ARMCC::GT)
2126 .Case("le", ARMCC::LE)
2127 .Case("al", ARMCC::AL)
2128 .Default(~0U);
2129 if (CC != ~0U) {
2130 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
2131 PredicationCode = CC;
2132 }
Bill Wendling52925b62010-10-29 23:50:21 +00002133 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002134
Daniel Dunbar352e1482011-01-11 15:59:50 +00002135 // Next, determine if we have a carry setting bit. We explicitly ignore all
2136 // the instructions we know end in 's'.
2137 if (Mnemonic.endswith("s") &&
2138 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002139 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
2140 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
2141 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
2142 Mnemonic == "vrsqrts" || (Mnemonic == "movs" && isThumb()))) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00002143 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
2144 CarrySetting = true;
2145 }
2146
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002147 // The "cps" instruction can have a interrupt mode operand which is glued into
2148 // the mnemonic. Check if this is the case, split it and parse the imod op
2149 if (Mnemonic.startswith("cps")) {
2150 // Split out any imod code.
2151 unsigned IMod =
2152 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
2153 .Case("ie", ARM_PROC::IE)
2154 .Case("id", ARM_PROC::ID)
2155 .Default(~0U);
2156 if (IMod != ~0U) {
2157 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2158 ProcessorIMod = IMod;
2159 }
2160 }
2161
Daniel Dunbar352e1482011-01-11 15:59:50 +00002162 return Mnemonic;
2163}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002164
2165/// \brief Given a canonical mnemonic, determine if the instruction ever allows
2166/// inclusion of carry set or predication code operands.
2167//
2168// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00002169void ARMAsmParser::
2170GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2171 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002172 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2173 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2174 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2175 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002176 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002177 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2178 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002179 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00002180 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002181 CanAcceptCarrySet = true;
2182 } else {
2183 CanAcceptCarrySet = false;
2184 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002185
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002186 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2187 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2188 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2189 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002190 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "clrex" ||
2191 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumb())) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002192 CanAcceptPredicationCode = false;
2193 } else {
2194 CanAcceptPredicationCode = true;
2195 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002196
Evan Chengebdeeab2011-07-08 01:53:10 +00002197 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002198 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002199 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002200 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002201}
2202
2203/// Parse an arm instruction mnemonic followed by its operands.
2204bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2205 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2206 // Create the leading tokens for the mnemonic, split by '.' characters.
2207 size_t Start = 0, Next = Name.find('.');
Jim Grosbachffa32252011-07-19 19:13:28 +00002208 StringRef Mnemonic = Name.slice(Start, Next);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002209
Daniel Dunbar352e1482011-01-11 15:59:50 +00002210 // Split out the predication code and carry setting flag from the mnemonic.
2211 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002212 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002213 bool CarrySetting;
Jim Grosbachffa32252011-07-19 19:13:28 +00002214 Mnemonic = SplitMnemonic(Mnemonic, PredicationCode, CarrySetting,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002215 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002216
Jim Grosbachffa32252011-07-19 19:13:28 +00002217 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
2218
2219 // FIXME: This is all a pretty gross hack. We should automatically handle
2220 // optional operands like this via tblgen.
Bill Wendling9717fa92010-11-21 10:56:05 +00002221
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002222 // Next, add the CCOut and ConditionCode operands, if needed.
2223 //
2224 // For mnemonics which can ever incorporate a carry setting bit or predication
2225 // code, our matching model involves us always generating CCOut and
2226 // ConditionCode operands to match the mnemonic "as written" and then we let
2227 // the matcher deal with finding the right instruction or generating an
2228 // appropriate error.
2229 bool CanAcceptCarrySet, CanAcceptPredicationCode;
Jim Grosbachffa32252011-07-19 19:13:28 +00002230 GetMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002231
Jim Grosbach33c16a22011-07-14 22:04:21 +00002232 // If we had a carry-set on an instruction that can't do that, issue an
2233 // error.
2234 if (!CanAcceptCarrySet && CarrySetting) {
2235 Parser.EatToEndOfStatement();
Jim Grosbachffa32252011-07-19 19:13:28 +00002236 return Error(NameLoc, "instruction '" + Mnemonic +
Jim Grosbach33c16a22011-07-14 22:04:21 +00002237 "' can not set flags, but 's' suffix specified");
2238 }
2239
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002240 // Add the carry setting operand, if necessary.
2241 //
2242 // FIXME: It would be awesome if we could somehow invent a location such that
2243 // match errors on this operand would print a nice diagnostic about how the
2244 // 's' character in the mnemonic resulted in a CCOut operand.
Jim Grosbach33c16a22011-07-14 22:04:21 +00002245 if (CanAcceptCarrySet)
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002246 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2247 NameLoc));
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002248
2249 // Add the predication code operand, if necessary.
2250 if (CanAcceptPredicationCode) {
2251 Operands.push_back(ARMOperand::CreateCondCode(
2252 ARMCC::CondCodes(PredicationCode), NameLoc));
2253 } else {
2254 // This mnemonic can't ever accept a predication code, but the user wrote
2255 // one (or misspelled another mnemonic).
2256
2257 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002258 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002259
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002260 // Add the processor imod operand, if necessary.
2261 if (ProcessorIMod) {
2262 Operands.push_back(ARMOperand::CreateImm(
2263 MCConstantExpr::Create(ProcessorIMod, getContext()),
2264 NameLoc, NameLoc));
2265 } else {
2266 // This mnemonic can't ever accept a imod, but the user wrote
2267 // one (or misspelled another mnemonic).
2268
2269 // FIXME: Issue a nice error.
2270 }
2271
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002272 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002273 while (Next != StringRef::npos) {
2274 Start = Next;
2275 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002276 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002277
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002278 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002279 }
2280
2281 // Read the remaining operands.
2282 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002283 // Read the first operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002284 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002285 Parser.EatToEndOfStatement();
2286 return true;
2287 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002288
2289 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002290 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002291
2292 // Parse and remember the operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002293 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002294 Parser.EatToEndOfStatement();
2295 return true;
2296 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002297 }
2298 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002299
Chris Lattnercbf8a982010-09-11 16:18:25 +00002300 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2301 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002302 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002303 }
Bill Wendling146018f2010-11-06 21:42:12 +00002304
Chris Lattner34e53142010-09-08 05:10:46 +00002305 Parser.Lex(); // Consume the EndOfStatement
Jim Grosbachffa32252011-07-19 19:13:28 +00002306
2307
2308 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
2309 // another does not. Specifically, the MOVW instruction does not. So we
2310 // special case it here and remove the defaulted (non-setting) cc_out
2311 // operand if that's the instruction we're trying to match.
2312 //
2313 // We do this post-processing of the explicit operands rather than just
2314 // conditionally adding the cc_out in the first place because we need
2315 // to check the type of the parsed immediate operand.
2316 if (Mnemonic == "mov" && Operands.size() > 4 &&
2317 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
Jim Grosbach731f2092011-07-19 19:45:44 +00002318 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
2319 static_cast<ARMOperand*>(Operands[1])->getReg() == 0) {
Jim Grosbachffa32252011-07-19 19:13:28 +00002320 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
2321 Operands.erase(Operands.begin() + 1);
2322 delete Op;
2323 }
2324
2325
2326
Chris Lattner98986712010-01-14 22:21:20 +00002327 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002328}
2329
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002330bool ARMAsmParser::
2331MatchAndEmitInstruction(SMLoc IDLoc,
2332 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2333 MCStreamer &Out) {
2334 MCInst Inst;
2335 unsigned ErrorInfo;
Jim Grosbach5a187002011-07-19 18:32:48 +00002336 MatchResultTy MatchResult;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002337 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002338 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002339 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002340 Out.EmitInstruction(Inst);
2341 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002342 case Match_MissingFeature:
2343 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2344 return true;
2345 case Match_InvalidOperand: {
2346 SMLoc ErrorLoc = IDLoc;
2347 if (ErrorInfo != ~0U) {
2348 if (ErrorInfo >= Operands.size())
2349 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002350
Chris Lattnere73d4f82010-10-28 21:41:58 +00002351 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2352 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2353 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002354
Chris Lattnere73d4f82010-10-28 21:41:58 +00002355 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002356 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002357 case Match_MnemonicFail:
2358 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002359 case Match_ConversionFail:
2360 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002361 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002362
Eric Christopherc223e2b2010-10-29 09:26:59 +00002363 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002364 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002365}
2366
Kevin Enderby515d5092009-10-15 20:48:48 +00002367/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002368bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2369 StringRef IDVal = DirectiveID.getIdentifier();
2370 if (IDVal == ".word")
2371 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002372 else if (IDVal == ".thumb")
2373 return ParseDirectiveThumb(DirectiveID.getLoc());
2374 else if (IDVal == ".thumb_func")
2375 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2376 else if (IDVal == ".code")
2377 return ParseDirectiveCode(DirectiveID.getLoc());
2378 else if (IDVal == ".syntax")
2379 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002380 return true;
2381}
2382
2383/// ParseDirectiveWord
2384/// ::= .word [ expression (, expression)* ]
2385bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2386 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2387 for (;;) {
2388 const MCExpr *Value;
2389 if (getParser().ParseExpression(Value))
2390 return true;
2391
Chris Lattneraaec2052010-01-19 19:46:13 +00002392 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002393
2394 if (getLexer().is(AsmToken::EndOfStatement))
2395 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002396
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002397 // FIXME: Improve diagnostic.
2398 if (getLexer().isNot(AsmToken::Comma))
2399 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002400 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002401 }
2402 }
2403
Sean Callananb9a25b72010-01-19 20:27:46 +00002404 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002405 return false;
2406}
2407
Kevin Enderby515d5092009-10-15 20:48:48 +00002408/// ParseDirectiveThumb
2409/// ::= .thumb
2410bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2411 if (getLexer().isNot(AsmToken::EndOfStatement))
2412 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002413 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002414
2415 // TODO: set thumb mode
2416 // TODO: tell the MC streamer the mode
2417 // getParser().getStreamer().Emit???();
2418 return false;
2419}
2420
2421/// ParseDirectiveThumbFunc
2422/// ::= .thumbfunc symbol_name
2423bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002424 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2425 bool isMachO = MAI.hasSubsectionsViaSymbols();
2426 StringRef Name;
2427
2428 // Darwin asm has function name after .thumb_func direction
2429 // ELF doesn't
2430 if (isMachO) {
2431 const AsmToken &Tok = Parser.getTok();
2432 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2433 return Error(L, "unexpected token in .thumb_func directive");
2434 Name = Tok.getString();
2435 Parser.Lex(); // Consume the identifier token.
2436 }
2437
Kevin Enderby515d5092009-10-15 20:48:48 +00002438 if (getLexer().isNot(AsmToken::EndOfStatement))
2439 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002440 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002441
Rafael Espindola64695402011-05-16 16:17:21 +00002442 // FIXME: assuming function name will be the line following .thumb_func
2443 if (!isMachO) {
2444 Name = Parser.getTok().getString();
2445 }
2446
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002447 // Mark symbol as a thumb symbol.
2448 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2449 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002450 return false;
2451}
2452
2453/// ParseDirectiveSyntax
2454/// ::= .syntax unified | divided
2455bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002456 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002457 if (Tok.isNot(AsmToken::Identifier))
2458 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002459 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002460 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002461 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002462 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002463 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002464 else
2465 return Error(L, "unrecognized syntax mode in .syntax directive");
2466
2467 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002468 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002469 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002470
2471 // TODO tell the MC streamer the mode
2472 // getParser().getStreamer().Emit???();
2473 return false;
2474}
2475
2476/// ParseDirectiveCode
2477/// ::= .code 16 | 32
2478bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002479 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002480 if (Tok.isNot(AsmToken::Integer))
2481 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002482 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002483 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002484 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002485 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002486 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002487 else
2488 return Error(L, "invalid operand to .code directive");
2489
2490 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002491 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002492 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002493
Evan Cheng32869202011-07-08 22:36:29 +00002494 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002495 if (!isThumb())
2496 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002497 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002498 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002499 if (isThumb())
2500 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002501 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002502 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002503
Kevin Enderby515d5092009-10-15 20:48:48 +00002504 return false;
2505}
2506
Sean Callanan90b70972010-04-07 20:29:34 +00002507extern "C" void LLVMInitializeARMAsmLexer();
2508
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002509/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002510extern "C" void LLVMInitializeARMAsmParser() {
2511 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2512 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002513 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002514}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002515
Chris Lattner0692ee62010-09-06 19:11:01 +00002516#define GET_REGISTER_MATCHER
2517#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002518#include "ARMGenAsmMatcher.inc"