blob: 1013ecf3ad8afd7618e5000303017d60a8b09803 [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"
Bill Wendling92b5a2e2010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Evan Cheng75972122011-01-13 07:58:56 +000012#include "ARMMCExpr.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000013#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000014#include "ARMSubtarget.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);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000082 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
83 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000084
Evan Chengebdeeab2011-07-08 01:53:10 +000085 bool isThumb() const {
86 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000087 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000088 }
Evan Chengebdeeab2011-07-08 01:53:10 +000089 bool isThumbOne() const {
Evan Chengffc0e732011-07-09 05:47:46 +000090 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000091 }
Evan Cheng32869202011-07-08 22:36:29 +000092 void SwitchMode() {
Evan Chengffc0e732011-07-09 05:47:46 +000093 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
94 setAvailableFeatures(FB);
Evan Cheng32869202011-07-08 22:36:29 +000095 }
Evan Chengebdeeab2011-07-08 01:53:10 +000096
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000097 /// @name Auto-generated Match Functions
98 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000099
Chris Lattner0692ee62010-09-06 19:11:01 +0000100#define GET_ASSEMBLER_HEADER
101#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000102
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000103 /// }
104
Jim Grosbachf922c472011-02-12 01:34:40 +0000105 OperandMatchResultTy tryParseCoprocNumOperand(
106 SmallVectorImpl<MCParsedAsmOperand*>&);
107 OperandMatchResultTy tryParseCoprocRegOperand(
108 SmallVectorImpl<MCParsedAsmOperand*>&);
109 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000110 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000111 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000112 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000113 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000114 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000115 OperandMatchResultTy tryParseMemMode2Operand(
116 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000117 OperandMatchResultTy tryParseMemMode3Operand(
118 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000119
120 // Asm Match Converter Methods
121 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
122 const SmallVectorImpl<MCParsedAsmOperand*> &);
123 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
124 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000125 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
126 const SmallVectorImpl<MCParsedAsmOperand*> &);
127 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
128 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000129
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000130public:
Evan Chengffc0e732011-07-09 05:47:46 +0000131 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
132 : TargetAsmParser(), STI(_STI), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000133 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000134
Evan Chengebdeeab2011-07-08 01:53:10 +0000135 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000136 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evan Chengebdeeab2011-07-08 01:53:10 +0000137 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000138
Benjamin Kramer38e59892010-07-14 22:38:02 +0000139 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000140 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000141 virtual bool ParseDirective(AsmToken DirectiveID);
142};
Jim Grosbach16c74252010-10-29 14:46:02 +0000143} // end anonymous namespace
144
Chris Lattner3a697562010-10-28 17:20:03 +0000145namespace {
146
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000147/// ARMOperand - Instances of this class represent a parsed ARM machine
148/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000149class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000150 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000151 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000152 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000153 CoprocNum,
154 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000155 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000156 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000157 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000158 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000159 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000160 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000161 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000162 DPRRegisterList,
163 SPRRegisterList,
Jim Grosbache8606dc2011-07-13 17:50:29 +0000164 ShiftedRegister,
Owen Anderson00828302011-03-18 22:50:18 +0000165 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000166 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000167 } Kind;
168
Sean Callanan76264762010-04-02 22:27:05 +0000169 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000170 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000171
172 union {
173 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000174 ARMCC::CondCodes Val;
175 } CC;
176
177 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000178 ARM_MB::MemBOpt Val;
179 } MBOpt;
180
181 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000182 unsigned Val;
183 } Cop;
184
185 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000186 ARM_PROC::IFlags Val;
187 } IFlags;
188
189 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000190 unsigned Val;
191 } MMask;
192
193 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000194 const char *Data;
195 unsigned Length;
196 } Tok;
197
198 struct {
199 unsigned RegNum;
200 } Reg;
201
Bill Wendling8155e5b2010-11-06 22:19:43 +0000202 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000203 const MCExpr *Val;
204 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000205
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000206 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000207 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000208 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000209 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000210 union {
211 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
212 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
213 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000214 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000215 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000216 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000217 unsigned Preindexed : 1;
218 unsigned Postindexed : 1;
219 unsigned OffsetIsReg : 1;
220 unsigned Negative : 1; // only used when OffsetIsReg is true
221 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000222 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000223
224 struct {
225 ARM_AM::ShiftOpc ShiftTy;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000226 unsigned Imm;
Owen Anderson00828302011-03-18 22:50:18 +0000227 } Shift;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000228 struct {
229 ARM_AM::ShiftOpc ShiftTy;
230 unsigned SrcReg;
231 unsigned ShiftReg;
232 unsigned ShiftImm;
233 } ShiftedReg;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000234 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000235
Bill Wendling146018f2010-11-06 21:42:12 +0000236 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
237public:
Sean Callanan76264762010-04-02 22:27:05 +0000238 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
239 Kind = o.Kind;
240 StartLoc = o.StartLoc;
241 EndLoc = o.EndLoc;
242 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000243 case CondCode:
244 CC = o.CC;
245 break;
Sean Callanan76264762010-04-02 22:27:05 +0000246 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000247 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000248 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000249 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000250 case Register:
251 Reg = o.Reg;
252 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000253 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000254 case DPRRegisterList:
255 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000256 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000257 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000258 case CoprocNum:
259 case CoprocReg:
260 Cop = o.Cop;
261 break;
Sean Callanan76264762010-04-02 22:27:05 +0000262 case Immediate:
263 Imm = o.Imm;
264 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000265 case MemBarrierOpt:
266 MBOpt = o.MBOpt;
267 break;
Sean Callanan76264762010-04-02 22:27:05 +0000268 case Memory:
269 Mem = o.Mem;
270 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000271 case MSRMask:
272 MMask = o.MMask;
273 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000274 case ProcIFlags:
275 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000276 break;
277 case Shifter:
278 Shift = o.Shift;
279 break;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000280 case ShiftedRegister:
281 ShiftedReg = o.ShiftedReg;
282 break;
Sean Callanan76264762010-04-02 22:27:05 +0000283 }
284 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000285
Sean Callanan76264762010-04-02 22:27:05 +0000286 /// getStartLoc - Get the location of the first token of this operand.
287 SMLoc getStartLoc() const { return StartLoc; }
288 /// getEndLoc - Get the location of the last token of this operand.
289 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000290
Daniel Dunbar8462b302010-08-11 06:36:53 +0000291 ARMCC::CondCodes getCondCode() const {
292 assert(Kind == CondCode && "Invalid access!");
293 return CC.Val;
294 }
295
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000296 unsigned getCoproc() const {
297 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
298 return Cop.Val;
299 }
300
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000301 StringRef getToken() const {
302 assert(Kind == Token && "Invalid access!");
303 return StringRef(Tok.Data, Tok.Length);
304 }
305
306 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000307 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000308 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000309 }
310
Bill Wendling5fa22a12010-11-09 23:28:44 +0000311 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000312 assert((Kind == RegisterList || Kind == DPRRegisterList ||
313 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000314 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000315 }
316
Kevin Enderbycfe07242009-10-13 22:19:02 +0000317 const MCExpr *getImm() const {
318 assert(Kind == Immediate && "Invalid access!");
319 return Imm.Val;
320 }
321
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000322 ARM_MB::MemBOpt getMemBarrierOpt() const {
323 assert(Kind == MemBarrierOpt && "Invalid access!");
324 return MBOpt.Val;
325 }
326
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000327 ARM_PROC::IFlags getProcIFlags() const {
328 assert(Kind == ProcIFlags && "Invalid access!");
329 return IFlags.Val;
330 }
331
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000332 unsigned getMSRMask() const {
333 assert(Kind == MSRMask && "Invalid access!");
334 return MMask.Val;
335 }
336
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000337 /// @name Memory Operand Accessors
338 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000339 ARMII::AddrMode getMemAddrMode() const {
340 return Mem.AddrMode;
341 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000342 unsigned getMemBaseRegNum() const {
343 return Mem.BaseRegNum;
344 }
345 unsigned getMemOffsetRegNum() const {
346 assert(Mem.OffsetIsReg && "Invalid access!");
347 return Mem.Offset.RegNum;
348 }
349 const MCExpr *getMemOffset() const {
350 assert(!Mem.OffsetIsReg && "Invalid access!");
351 return Mem.Offset.Value;
352 }
353 unsigned getMemOffsetRegShifted() const {
354 assert(Mem.OffsetIsReg && "Invalid access!");
355 return Mem.OffsetRegShifted;
356 }
357 const MCExpr *getMemShiftAmount() const {
358 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
359 return Mem.ShiftAmount;
360 }
Owen Anderson00828302011-03-18 22:50:18 +0000361 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000362 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
363 return Mem.ShiftType;
364 }
365 bool getMemPreindexed() const { return Mem.Preindexed; }
366 bool getMemPostindexed() const { return Mem.Postindexed; }
367 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
368 bool getMemNegative() const { return Mem.Negative; }
369 bool getMemWriteback() const { return Mem.Writeback; }
370
371 /// @}
372
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000373 bool isCoprocNum() const { return Kind == CoprocNum; }
374 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000375 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000376 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000377 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000378 bool isImm0_255() const {
379 if (Kind != Immediate)
380 return false;
381 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
382 if (!CE) return false;
383 int64_t Value = CE->getValue();
384 return Value >= 0 && Value < 256;
385 }
Jim Grosbach83ab0702011-07-13 22:01:08 +0000386 bool isImm0_7() const {
387 if (Kind != Immediate)
388 return false;
389 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
390 if (!CE) return false;
391 int64_t Value = CE->getValue();
392 return Value >= 0 && Value < 8;
393 }
394 bool isImm0_15() const {
395 if (Kind != Immediate)
396 return false;
397 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
398 if (!CE) return false;
399 int64_t Value = CE->getValue();
400 return Value >= 0 && Value < 16;
401 }
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000402 bool isImm0_65535() const {
403 if (Kind != Immediate)
404 return false;
405 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
406 if (!CE) return false;
407 int64_t Value = CE->getValue();
408 return Value >= 0 && Value < 65536;
409 }
Jim Grosbachffa32252011-07-19 19:13:28 +0000410 bool isImm0_65535Expr() const {
411 if (Kind != Immediate)
412 return false;
413 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
414 // If it's not a constant expression, it'll generate a fixup and be
415 // handled later.
416 if (!CE) return true;
417 int64_t Value = CE->getValue();
418 return Value >= 0 && Value < 65536;
419 }
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000420 bool isARMSOImm() const {
421 if (Kind != Immediate)
422 return false;
423 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
424 if (!CE) return false;
425 int64_t Value = CE->getValue();
426 return ARM_AM::getSOImmVal(Value) != -1;
427 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000428 bool isT2SOImm() const {
429 if (Kind != Immediate)
430 return false;
431 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
432 if (!CE) return false;
433 int64_t Value = CE->getValue();
434 return ARM_AM::getT2SOImmVal(Value) != -1;
435 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000436 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000437 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000438 bool isDPRRegList() const { return Kind == DPRRegisterList; }
439 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000440 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000441 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000442 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000443 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000444 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000445 bool isMemMode2() const {
446 if (getMemAddrMode() != ARMII::AddrMode2)
447 return false;
448
449 if (getMemOffsetIsReg())
450 return true;
451
452 if (getMemNegative() &&
453 !(getMemPostindexed() || getMemPreindexed()))
454 return false;
455
456 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
457 if (!CE) return false;
458 int64_t Value = CE->getValue();
459
460 // The offset must be in the range 0-4095 (imm12).
461 if (Value > 4095 || Value < -4095)
462 return false;
463
464 return true;
465 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000466 bool isMemMode3() const {
467 if (getMemAddrMode() != ARMII::AddrMode3)
468 return false;
469
470 if (getMemOffsetIsReg()) {
471 if (getMemOffsetRegShifted())
472 return false; // No shift with offset reg allowed
473 return true;
474 }
475
476 if (getMemNegative() &&
477 !(getMemPostindexed() || getMemPreindexed()))
478 return false;
479
480 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
481 if (!CE) return false;
482 int64_t Value = CE->getValue();
483
484 // The offset must be in the range 0-255 (imm8).
485 if (Value > 255 || Value < -255)
486 return false;
487
488 return true;
489 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000490 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000491 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
492 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000493 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000494
Daniel Dunbar4b462672011-01-18 05:55:27 +0000495 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000496 if (!CE) return false;
497
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000498 // The offset must be a multiple of 4 in the range 0-1020.
499 int64_t Value = CE->getValue();
500 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
501 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000502 bool isMemMode7() const {
503 if (!isMemory() ||
504 getMemPreindexed() ||
505 getMemPostindexed() ||
506 getMemOffsetIsReg() ||
507 getMemNegative() ||
508 getMemWriteback())
509 return false;
510
511 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
512 if (!CE) return false;
513
514 if (CE->getValue())
515 return false;
516
517 return true;
518 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000519 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000520 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000521 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000522 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000523 }
524 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000525 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000526 return false;
527
Daniel Dunbar4b462672011-01-18 05:55:27 +0000528 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000529 if (!CE) return false;
530
531 // The offset must be a multiple of 4 in the range 0-124.
532 uint64_t Value = CE->getValue();
533 return ((Value & 0x3) == 0 && Value <= 124);
534 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000535 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000536 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000537
538 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000539 // Add as immediates when possible. Null MCExpr = 0.
540 if (Expr == 0)
541 Inst.addOperand(MCOperand::CreateImm(0));
542 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000543 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
544 else
545 Inst.addOperand(MCOperand::CreateExpr(Expr));
546 }
547
Daniel Dunbar8462b302010-08-11 06:36:53 +0000548 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000549 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000550 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000551 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
552 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000553 }
554
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000555 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
556 assert(N == 1 && "Invalid number of operands!");
557 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
558 }
559
560 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
561 assert(N == 1 && "Invalid number of operands!");
562 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
563 }
564
Jim Grosbachd67641b2010-12-06 18:21:12 +0000565 void addCCOutOperands(MCInst &Inst, unsigned N) const {
566 assert(N == 1 && "Invalid number of operands!");
567 Inst.addOperand(MCOperand::CreateReg(getReg()));
568 }
569
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000570 void addRegOperands(MCInst &Inst, unsigned N) const {
571 assert(N == 1 && "Invalid number of operands!");
572 Inst.addOperand(MCOperand::CreateReg(getReg()));
573 }
574
Jim Grosbache8606dc2011-07-13 17:50:29 +0000575 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
576 assert(N == 3 && "Invalid number of operands!");
577 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
578 assert((ShiftedReg.ShiftReg == 0 ||
579 ARM_AM::getSORegOffset(ShiftedReg.ShiftImm) == 0) &&
580 "Invalid shifted register operand!");
581 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
582 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
583 Inst.addOperand(MCOperand::CreateImm(
584 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
585 }
586
Owen Anderson00828302011-03-18 22:50:18 +0000587 void addShifterOperands(MCInst &Inst, unsigned N) const {
588 assert(N == 1 && "Invalid number of operands!");
589 Inst.addOperand(MCOperand::CreateImm(
590 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
591 }
592
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000593 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000594 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000595 const SmallVectorImpl<unsigned> &RegList = getRegList();
596 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000597 I = RegList.begin(), E = RegList.end(); I != E; ++I)
598 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000599 }
600
Bill Wendling0f630752010-11-17 04:32:08 +0000601 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
602 addRegListOperands(Inst, N);
603 }
604
605 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
606 addRegListOperands(Inst, N);
607 }
608
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000609 void addImmOperands(MCInst &Inst, unsigned N) const {
610 assert(N == 1 && "Invalid number of operands!");
611 addExpr(Inst, getImm());
612 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000613
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000614 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
615 assert(N == 1 && "Invalid number of operands!");
616 addExpr(Inst, getImm());
617 }
618
Jim Grosbach83ab0702011-07-13 22:01:08 +0000619 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
620 assert(N == 1 && "Invalid number of operands!");
621 addExpr(Inst, getImm());
622 }
623
624 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
625 assert(N == 1 && "Invalid number of operands!");
626 addExpr(Inst, getImm());
627 }
628
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000629 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
630 assert(N == 1 && "Invalid number of operands!");
631 addExpr(Inst, getImm());
632 }
633
Jim Grosbachffa32252011-07-19 19:13:28 +0000634 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
635 assert(N == 1 && "Invalid number of operands!");
636 addExpr(Inst, getImm());
637 }
638
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000639 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
640 assert(N == 1 && "Invalid number of operands!");
641 addExpr(Inst, getImm());
642 }
643
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000644 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
645 assert(N == 1 && "Invalid number of operands!");
646 addExpr(Inst, getImm());
647 }
648
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000649 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
650 assert(N == 1 && "Invalid number of operands!");
651 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
652 }
653
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000654 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
655 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
656 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
657
658 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000659 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000660 assert((CE || CE->getValue() == 0) &&
661 "No offset operand support in mode 7");
662 }
663
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000664 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
665 assert(isMemMode2() && "Invalid mode or number of operands!");
666 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
667 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
668
669 if (getMemOffsetIsReg()) {
670 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
671
672 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
673 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
674 int64_t ShiftAmount = 0;
675
676 if (getMemOffsetRegShifted()) {
677 ShOpc = getMemShiftType();
678 const MCConstantExpr *CE =
679 dyn_cast<MCConstantExpr>(getMemShiftAmount());
680 ShiftAmount = CE->getValue();
681 }
682
683 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
684 ShOpc, IdxMode)));
685 return;
686 }
687
688 // Create a operand placeholder to always yield the same number of operands.
689 Inst.addOperand(MCOperand::CreateReg(0));
690
691 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
692 // the difference?
693 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
694 assert(CE && "Non-constant mode 2 offset operand!");
695 int64_t Offset = CE->getValue();
696
697 if (Offset >= 0)
698 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
699 Offset, ARM_AM::no_shift, IdxMode)));
700 else
701 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
702 -Offset, ARM_AM::no_shift, IdxMode)));
703 }
704
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000705 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
706 assert(isMemMode3() && "Invalid mode or number of operands!");
707 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
708 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
709
710 if (getMemOffsetIsReg()) {
711 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
712
713 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
714 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
715 IdxMode)));
716 return;
717 }
718
719 // Create a operand placeholder to always yield the same number of operands.
720 Inst.addOperand(MCOperand::CreateReg(0));
721
722 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
723 // the difference?
724 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
725 assert(CE && "Non-constant mode 3 offset operand!");
726 int64_t Offset = CE->getValue();
727
728 if (Offset >= 0)
729 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
730 Offset, IdxMode)));
731 else
732 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
733 -Offset, IdxMode)));
734 }
735
Chris Lattner14b93852010-10-29 00:27:31 +0000736 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
737 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000738
Daniel Dunbar4b462672011-01-18 05:55:27 +0000739 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
740 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000741
Jim Grosbach80eb2332010-10-29 17:41:25 +0000742 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
743 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000744 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000745 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000746
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000747 // The MCInst offset operand doesn't include the low two bits (like
748 // the instruction encoding).
749 int64_t Offset = CE->getValue() / 4;
750 if (Offset >= 0)
751 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
752 Offset)));
753 else
754 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
755 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000756 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000757
Bill Wendlingf4caf692010-12-14 03:36:38 +0000758 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
759 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000760 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
761 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000762 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000763
Bill Wendlingf4caf692010-12-14 03:36:38 +0000764 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
765 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000766 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
767 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000768 assert(CE && "Non-constant mode offset operand!");
769 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000770 }
771
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000772 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
773 assert(N == 1 && "Invalid number of operands!");
774 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
775 }
776
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000777 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
778 assert(N == 1 && "Invalid number of operands!");
779 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
780 }
781
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000782 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000783
Chris Lattner3a697562010-10-28 17:20:03 +0000784 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
785 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000786 Op->CC.Val = CC;
787 Op->StartLoc = S;
788 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000789 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000790 }
791
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000792 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
793 ARMOperand *Op = new ARMOperand(CoprocNum);
794 Op->Cop.Val = CopVal;
795 Op->StartLoc = S;
796 Op->EndLoc = S;
797 return Op;
798 }
799
800 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
801 ARMOperand *Op = new ARMOperand(CoprocReg);
802 Op->Cop.Val = CopVal;
803 Op->StartLoc = S;
804 Op->EndLoc = S;
805 return Op;
806 }
807
Jim Grosbachd67641b2010-12-06 18:21:12 +0000808 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
809 ARMOperand *Op = new ARMOperand(CCOut);
810 Op->Reg.RegNum = RegNum;
811 Op->StartLoc = S;
812 Op->EndLoc = S;
813 return Op;
814 }
815
Chris Lattner3a697562010-10-28 17:20:03 +0000816 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
817 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000818 Op->Tok.Data = Str.data();
819 Op->Tok.Length = Str.size();
820 Op->StartLoc = S;
821 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000822 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000823 }
824
Bill Wendling50d0f582010-11-18 23:43:05 +0000825 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000826 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000827 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000828 Op->StartLoc = S;
829 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000830 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000831 }
832
Jim Grosbache8606dc2011-07-13 17:50:29 +0000833 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
834 unsigned SrcReg,
835 unsigned ShiftReg,
836 unsigned ShiftImm,
837 SMLoc S, SMLoc E) {
838 ARMOperand *Op = new ARMOperand(ShiftedRegister);
839 Op->ShiftedReg.ShiftTy = ShTy;
840 Op->ShiftedReg.SrcReg = SrcReg;
841 Op->ShiftedReg.ShiftReg = ShiftReg;
842 Op->ShiftedReg.ShiftImm = ShiftImm;
843 Op->StartLoc = S;
844 Op->EndLoc = E;
845 return Op;
846 }
847
Owen Anderson00828302011-03-18 22:50:18 +0000848 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
849 SMLoc S, SMLoc E) {
850 ARMOperand *Op = new ARMOperand(Shifter);
851 Op->Shift.ShiftTy = ShTy;
852 Op->StartLoc = S;
853 Op->EndLoc = E;
854 return Op;
855 }
856
Bill Wendling7729e062010-11-09 22:44:22 +0000857 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000858 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000859 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000860 KindTy Kind = RegisterList;
861
862 if (ARM::DPRRegClass.contains(Regs.front().first))
863 Kind = DPRRegisterList;
864 else if (ARM::SPRRegClass.contains(Regs.front().first))
865 Kind = SPRRegisterList;
866
867 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000868 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000869 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000870 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000871 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000872 Op->StartLoc = StartLoc;
873 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000874 return Op;
875 }
876
Chris Lattner3a697562010-10-28 17:20:03 +0000877 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
878 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000879 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000880 Op->StartLoc = S;
881 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000882 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000883 }
884
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000885 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
886 bool OffsetIsReg, const MCExpr *Offset,
887 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000888 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000889 const MCExpr *ShiftAmount, bool Preindexed,
890 bool Postindexed, bool Negative, bool Writeback,
891 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000892 assert((OffsetRegNum == -1 || OffsetIsReg) &&
893 "OffsetRegNum must imply OffsetIsReg!");
894 assert((!OffsetRegShifted || OffsetIsReg) &&
895 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000896 assert((Offset || OffsetIsReg) &&
897 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000898 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
899 "Cannot have shift amount without shifted register offset!");
900 assert((!Offset || !OffsetIsReg) &&
901 "Cannot have expression offset and register offset!");
902
Chris Lattner3a697562010-10-28 17:20:03 +0000903 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000904 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000905 Op->Mem.BaseRegNum = BaseRegNum;
906 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000907 if (OffsetIsReg)
908 Op->Mem.Offset.RegNum = OffsetRegNum;
909 else
910 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000911 Op->Mem.OffsetRegShifted = OffsetRegShifted;
912 Op->Mem.ShiftType = ShiftType;
913 Op->Mem.ShiftAmount = ShiftAmount;
914 Op->Mem.Preindexed = Preindexed;
915 Op->Mem.Postindexed = Postindexed;
916 Op->Mem.Negative = Negative;
917 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000918
Sean Callanan76264762010-04-02 22:27:05 +0000919 Op->StartLoc = S;
920 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000921 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000922 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000923
924 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
925 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
926 Op->MBOpt.Val = Opt;
927 Op->StartLoc = S;
928 Op->EndLoc = S;
929 return Op;
930 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000931
932 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
933 ARMOperand *Op = new ARMOperand(ProcIFlags);
934 Op->IFlags.Val = IFlags;
935 Op->StartLoc = S;
936 Op->EndLoc = S;
937 return Op;
938 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000939
940 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
941 ARMOperand *Op = new ARMOperand(MSRMask);
942 Op->MMask.Val = MMask;
943 Op->StartLoc = S;
944 Op->EndLoc = S;
945 return Op;
946 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000947};
948
949} // end anonymous namespace.
950
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000951void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000952 switch (Kind) {
953 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000954 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000955 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000956 case CCOut:
957 OS << "<ccout " << getReg() << ">";
958 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000959 case CoprocNum:
960 OS << "<coprocessor number: " << getCoproc() << ">";
961 break;
962 case CoprocReg:
963 OS << "<coprocessor register: " << getCoproc() << ">";
964 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000965 case MSRMask:
966 OS << "<mask: " << getMSRMask() << ">";
967 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000968 case Immediate:
969 getImm()->print(OS);
970 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000971 case MemBarrierOpt:
972 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
973 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000974 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000975 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000976 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
977 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000978 if (getMemOffsetIsReg()) {
979 OS << " offset:<register " << getMemOffsetRegNum();
980 if (getMemOffsetRegShifted()) {
981 OS << " offset-shift-type:" << getMemShiftType();
982 OS << " offset-shift-amount:" << *getMemShiftAmount();
983 }
984 } else {
985 OS << " offset:" << *getMemOffset();
986 }
987 if (getMemOffsetIsReg())
988 OS << " (offset-is-reg)";
989 if (getMemPreindexed())
990 OS << " (pre-indexed)";
991 if (getMemPostindexed())
992 OS << " (post-indexed)";
993 if (getMemNegative())
994 OS << " (negative)";
995 if (getMemWriteback())
996 OS << " (writeback)";
997 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000998 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000999 case ProcIFlags: {
1000 OS << "<ARM_PROC::";
1001 unsigned IFlags = getProcIFlags();
1002 for (int i=2; i >= 0; --i)
1003 if (IFlags & (1 << i))
1004 OS << ARM_PROC::IFlagsToString(1 << i);
1005 OS << ">";
1006 break;
1007 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001008 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +00001009 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001010 break;
Owen Anderson00828302011-03-18 22:50:18 +00001011 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +00001012 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
1013 break;
1014 case ShiftedRegister:
1015 OS << "<so_reg"
1016 << ShiftedReg.SrcReg
1017 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
1018 << ", " << ShiftedReg.ShiftReg << ", "
1019 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
1020 << ">";
Owen Anderson00828302011-03-18 22:50:18 +00001021 break;
Bill Wendling0f630752010-11-17 04:32:08 +00001022 case RegisterList:
1023 case DPRRegisterList:
1024 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00001025 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001026
Bill Wendling5fa22a12010-11-09 23:28:44 +00001027 const SmallVectorImpl<unsigned> &RegList = getRegList();
1028 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001029 I = RegList.begin(), E = RegList.end(); I != E; ) {
1030 OS << *I;
1031 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001032 }
1033
1034 OS << ">";
1035 break;
1036 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001037 case Token:
1038 OS << "'" << getToken() << "'";
1039 break;
1040 }
1041}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001042
1043/// @name Auto-generated Match Functions
1044/// {
1045
1046static unsigned MatchRegisterName(StringRef Name);
1047
1048/// }
1049
Bob Wilson69df7232011-02-03 21:46:10 +00001050bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1051 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +00001052 RegNo = TryParseRegister();
1053
1054 return (RegNo == (unsigned)-1);
1055}
1056
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001057/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00001058/// and if it is a register name the token is eaten and the register number is
1059/// returned. Otherwise return -1.
1060///
1061int ARMAsmParser::TryParseRegister() {
1062 const AsmToken &Tok = Parser.getTok();
1063 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001064
Chris Lattnere5658fa2010-10-30 04:09:10 +00001065 // FIXME: Validate register for the current architecture; we have to do
1066 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001067 std::string upperCase = Tok.getString().str();
1068 std::string lowerCase = LowercaseString(upperCase);
1069 unsigned RegNum = MatchRegisterName(lowerCase);
1070 if (!RegNum) {
1071 RegNum = StringSwitch<unsigned>(lowerCase)
1072 .Case("r13", ARM::SP)
1073 .Case("r14", ARM::LR)
1074 .Case("r15", ARM::PC)
1075 .Case("ip", ARM::R12)
1076 .Default(0);
1077 }
1078 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001079
Chris Lattnere5658fa2010-10-30 04:09:10 +00001080 Parser.Lex(); // Eat identifier token.
1081 return RegNum;
1082}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001083
Jim Grosbach19906722011-07-13 18:49:30 +00001084// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1085// If a recoverable error occurs, return 1. If an irrecoverable error
1086// occurs, return -1. An irrecoverable error is one where tokens have been
1087// consumed in the process of trying to parse the shifter (i.e., when it is
1088// indeed a shifter operand, but malformed).
1089int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001090 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1091 SMLoc S = Parser.getTok().getLoc();
1092 const AsmToken &Tok = Parser.getTok();
1093 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1094
1095 std::string upperCase = Tok.getString().str();
1096 std::string lowerCase = LowercaseString(upperCase);
1097 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1098 .Case("lsl", ARM_AM::lsl)
1099 .Case("lsr", ARM_AM::lsr)
1100 .Case("asr", ARM_AM::asr)
1101 .Case("ror", ARM_AM::ror)
1102 .Case("rrx", ARM_AM::rrx)
1103 .Default(ARM_AM::no_shift);
1104
1105 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001106 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001107
Jim Grosbache8606dc2011-07-13 17:50:29 +00001108 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001109
Jim Grosbache8606dc2011-07-13 17:50:29 +00001110 // The source register for the shift has already been added to the
1111 // operand list, so we need to pop it off and combine it into the shifted
1112 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00001113 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00001114 if (!PrevOp->isReg())
1115 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1116 int SrcReg = PrevOp->getReg();
1117 int64_t Imm = 0;
1118 int ShiftReg = 0;
1119 if (ShiftTy == ARM_AM::rrx) {
1120 // RRX Doesn't have an explicit shift amount. The encoder expects
1121 // the shift register to be the same as the source register. Seems odd,
1122 // but OK.
1123 ShiftReg = SrcReg;
1124 } else {
1125 // Figure out if this is shifted by a constant or a register (for non-RRX).
1126 if (Parser.getTok().is(AsmToken::Hash)) {
1127 Parser.Lex(); // Eat hash.
1128 SMLoc ImmLoc = Parser.getTok().getLoc();
1129 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001130 if (getParser().ParseExpression(ShiftExpr)) {
1131 Error(ImmLoc, "invalid immediate shift value");
1132 return -1;
1133 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001134 // The expression must be evaluatable as an immediate.
1135 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001136 if (!CE) {
1137 Error(ImmLoc, "invalid immediate shift value");
1138 return -1;
1139 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001140 // Range check the immediate.
1141 // lsl, ror: 0 <= imm <= 31
1142 // lsr, asr: 0 <= imm <= 32
1143 Imm = CE->getValue();
1144 if (Imm < 0 ||
1145 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1146 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001147 Error(ImmLoc, "immediate shift value out of range");
1148 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001149 }
1150 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1151 ShiftReg = TryParseRegister();
1152 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001153 if (ShiftReg == -1) {
1154 Error (L, "expected immediate or register in shift operand");
1155 return -1;
1156 }
1157 } else {
1158 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001159 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001160 return -1;
1161 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001162 }
1163
Jim Grosbache8606dc2011-07-13 17:50:29 +00001164 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1165 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001166 S, Parser.getTok().getLoc()));
1167
Jim Grosbach19906722011-07-13 18:49:30 +00001168 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001169}
1170
1171
Bill Wendling50d0f582010-11-18 23:43:05 +00001172/// Try to parse a register name. The token must be an Identifier when called.
1173/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1174/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001175///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001176/// TODO this is likely to change to allow different register types and or to
1177/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001178bool ARMAsmParser::
1179TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001180 SMLoc S = Parser.getTok().getLoc();
1181 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001182 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001183 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001184
Bill Wendling50d0f582010-11-18 23:43:05 +00001185 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001186
Chris Lattnere5658fa2010-10-30 04:09:10 +00001187 const AsmToken &ExclaimTok = Parser.getTok();
1188 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001189 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1190 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001191 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001192 }
1193
Bill Wendling50d0f582010-11-18 23:43:05 +00001194 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001195}
1196
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001197/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1198/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1199/// "c5", ...
1200static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001201 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1202 // but efficient.
1203 switch (Name.size()) {
1204 default: break;
1205 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001206 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001207 return -1;
1208 switch (Name[1]) {
1209 default: return -1;
1210 case '0': return 0;
1211 case '1': return 1;
1212 case '2': return 2;
1213 case '3': return 3;
1214 case '4': return 4;
1215 case '5': return 5;
1216 case '6': return 6;
1217 case '7': return 7;
1218 case '8': return 8;
1219 case '9': return 9;
1220 }
1221 break;
1222 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001223 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001224 return -1;
1225 switch (Name[2]) {
1226 default: return -1;
1227 case '0': return 10;
1228 case '1': return 11;
1229 case '2': return 12;
1230 case '3': return 13;
1231 case '4': return 14;
1232 case '5': return 15;
1233 }
1234 break;
1235 }
1236
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001237 return -1;
1238}
1239
Jim Grosbachf922c472011-02-12 01:34:40 +00001240/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001241/// token must be an Identifier when called, and if it is a coprocessor
1242/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001243ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1244tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001245 SMLoc S = Parser.getTok().getLoc();
1246 const AsmToken &Tok = Parser.getTok();
1247 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1248
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001249 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001250 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001251 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001252
1253 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001254 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001255 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001256}
1257
Jim Grosbachf922c472011-02-12 01:34:40 +00001258/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001259/// token must be an Identifier when called, and if it is a coprocessor
1260/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001261ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1262tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001263 SMLoc S = Parser.getTok().getLoc();
1264 const AsmToken &Tok = Parser.getTok();
1265 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1266
1267 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1268 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001269 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001270
1271 Parser.Lex(); // Eat identifier token.
1272 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001273 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001274}
1275
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001276/// Parse a register list, return it if successful else return null. The first
1277/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001278bool ARMAsmParser::
1279ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001280 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001281 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001282 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001283
Bill Wendling7729e062010-11-09 22:44:22 +00001284 // Read the rest of the registers in the list.
1285 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001286 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001287
Bill Wendling7729e062010-11-09 22:44:22 +00001288 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001289 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001290 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001291
Sean Callanan18b83232010-01-19 21:44:56 +00001292 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001293 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001294 if (RegTok.isNot(AsmToken::Identifier)) {
1295 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001296 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001297 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001298
Bill Wendling1d6a2652010-11-06 10:40:24 +00001299 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001300 if (RegNum == -1) {
1301 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001302 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001303 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001304
Bill Wendlinge7176102010-11-06 22:36:58 +00001305 if (IsRange) {
1306 int Reg = PrevRegNum;
1307 do {
1308 ++Reg;
1309 Registers.push_back(std::make_pair(Reg, RegLoc));
1310 } while (Reg != RegNum);
1311 } else {
1312 Registers.push_back(std::make_pair(RegNum, RegLoc));
1313 }
1314
1315 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001316 } while (Parser.getTok().is(AsmToken::Comma) ||
1317 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001318
1319 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001320 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001321 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1322 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001323 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001324 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001325
Bill Wendlinge7176102010-11-06 22:36:58 +00001326 SMLoc E = RCurlyTok.getLoc();
1327 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001328
Bill Wendlinge7176102010-11-06 22:36:58 +00001329 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001330 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001331 RI = Registers.begin(), RE = Registers.end();
1332
Bill Wendling7caebff2011-01-12 21:20:59 +00001333 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001334 bool EmittedWarning = false;
1335
Bill Wendling7caebff2011-01-12 21:20:59 +00001336 DenseMap<unsigned, bool> RegMap;
1337 RegMap[HighRegNum] = true;
1338
Bill Wendlinge7176102010-11-06 22:36:58 +00001339 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001340 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001341 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001342
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001343 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001344 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001345 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001346 }
1347
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001348 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001349 Warning(RegInfo.second,
1350 "register not in ascending order in register list");
1351
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001352 RegMap[Reg] = true;
1353 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001354 }
1355
Bill Wendling50d0f582010-11-18 23:43:05 +00001356 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1357 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001358}
1359
Jim Grosbachf922c472011-02-12 01:34:40 +00001360/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1361ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1362tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001363 SMLoc S = Parser.getTok().getLoc();
1364 const AsmToken &Tok = Parser.getTok();
1365 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1366 StringRef OptStr = Tok.getString();
1367
1368 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1369 .Case("sy", ARM_MB::SY)
1370 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001371 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001372 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001373 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001374 .Case("ishst", ARM_MB::ISHST)
1375 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001376 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001377 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001378 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001379 .Case("osh", ARM_MB::OSH)
1380 .Case("oshst", ARM_MB::OSHST)
1381 .Default(~0U);
1382
1383 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001384 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001385
1386 Parser.Lex(); // Eat identifier token.
1387 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001388 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001389}
1390
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001391/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001392ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1393tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1394 SMLoc S = Parser.getTok().getLoc();
1395 const AsmToken &Tok = Parser.getTok();
1396 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1397 StringRef IFlagsStr = Tok.getString();
1398
1399 unsigned IFlags = 0;
1400 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1401 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1402 .Case("a", ARM_PROC::A)
1403 .Case("i", ARM_PROC::I)
1404 .Case("f", ARM_PROC::F)
1405 .Default(~0U);
1406
1407 // If some specific iflag is already set, it means that some letter is
1408 // present more than once, this is not acceptable.
1409 if (Flag == ~0U || (IFlags & Flag))
1410 return MatchOperand_NoMatch;
1411
1412 IFlags |= Flag;
1413 }
1414
1415 Parser.Lex(); // Eat identifier token.
1416 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1417 return MatchOperand_Success;
1418}
1419
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001420/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1421ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1422tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1423 SMLoc S = Parser.getTok().getLoc();
1424 const AsmToken &Tok = Parser.getTok();
1425 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1426 StringRef Mask = Tok.getString();
1427
1428 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1429 size_t Start = 0, Next = Mask.find('_');
1430 StringRef Flags = "";
1431 StringRef SpecReg = Mask.slice(Start, Next);
1432 if (Next != StringRef::npos)
1433 Flags = Mask.slice(Next+1, Mask.size());
1434
1435 // FlagsVal contains the complete mask:
1436 // 3-0: Mask
1437 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1438 unsigned FlagsVal = 0;
1439
1440 if (SpecReg == "apsr") {
1441 FlagsVal = StringSwitch<unsigned>(Flags)
1442 .Case("nzcvq", 0x8) // same as CPSR_c
1443 .Case("g", 0x4) // same as CPSR_s
1444 .Case("nzcvqg", 0xc) // same as CPSR_fs
1445 .Default(~0U);
1446
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001447 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001448 if (!Flags.empty())
1449 return MatchOperand_NoMatch;
1450 else
1451 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001452 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001453 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001454 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1455 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001456 for (int i = 0, e = Flags.size(); i != e; ++i) {
1457 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1458 .Case("c", 1)
1459 .Case("x", 2)
1460 .Case("s", 4)
1461 .Case("f", 8)
1462 .Default(~0U);
1463
1464 // If some specific flag is already set, it means that some letter is
1465 // present more than once, this is not acceptable.
1466 if (FlagsVal == ~0U || (FlagsVal & Flag))
1467 return MatchOperand_NoMatch;
1468 FlagsVal |= Flag;
1469 }
1470 } else // No match for special register.
1471 return MatchOperand_NoMatch;
1472
1473 // Special register without flags are equivalent to "fc" flags.
1474 if (!FlagsVal)
1475 FlagsVal = 0x9;
1476
1477 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1478 if (SpecReg == "spsr")
1479 FlagsVal |= 16;
1480
1481 Parser.Lex(); // Eat identifier token.
1482 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1483 return MatchOperand_Success;
1484}
1485
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001486/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1487ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1488tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001489 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001490
1491 if (ParseMemory(Operands, ARMII::AddrMode2))
1492 return MatchOperand_NoMatch;
1493
1494 return MatchOperand_Success;
1495}
1496
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001497/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1498ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1499tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1500 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1501
1502 if (ParseMemory(Operands, ARMII::AddrMode3))
1503 return MatchOperand_NoMatch;
1504
1505 return MatchOperand_Success;
1506}
1507
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001508/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1509/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1510/// when they refer multiple MIOperands inside a single one.
1511bool ARMAsmParser::
1512CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1513 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1514 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1515
1516 // Create a writeback register dummy placeholder.
1517 Inst.addOperand(MCOperand::CreateImm(0));
1518
1519 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1520 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1521 return true;
1522}
1523
1524/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1525/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1526/// when they refer multiple MIOperands inside a single one.
1527bool ARMAsmParser::
1528CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1529 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1530 // Create a writeback register dummy placeholder.
1531 Inst.addOperand(MCOperand::CreateImm(0));
1532 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1533 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1534 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1535 return true;
1536}
1537
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001538/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1539/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1540/// when they refer multiple MIOperands inside a single one.
1541bool ARMAsmParser::
1542CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1543 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1544 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1545
1546 // Create a writeback register dummy placeholder.
1547 Inst.addOperand(MCOperand::CreateImm(0));
1548
1549 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1550 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1551 return true;
1552}
1553
1554/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1555/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1556/// when they refer multiple MIOperands inside a single one.
1557bool ARMAsmParser::
1558CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1559 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1560 // Create a writeback register dummy placeholder.
1561 Inst.addOperand(MCOperand::CreateImm(0));
1562 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1563 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1564 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1565 return true;
1566}
1567
Bill Wendlinge7176102010-11-06 22:36:58 +00001568/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001569/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001570///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001571/// TODO Only preindexing and postindexing addressing are started, unindexed
1572/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001573bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001574ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1575 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001576 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001577 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001578 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001579 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001580 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001581
Sean Callanan18b83232010-01-19 21:44:56 +00001582 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001583 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1584 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001585 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001586 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001587 int BaseRegNum = TryParseRegister();
1588 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001589 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001590 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001591 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001592
Daniel Dunbar05710932011-01-18 05:34:17 +00001593 // The next token must either be a comma or a closing bracket.
1594 const AsmToken &Tok = Parser.getTok();
1595 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1596 return true;
1597
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001598 bool Preindexed = false;
1599 bool Postindexed = false;
1600 bool OffsetIsReg = false;
1601 bool Negative = false;
1602 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001603 ARMOperand *WBOp = 0;
1604 int OffsetRegNum = -1;
1605 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001606 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001607 const MCExpr *ShiftAmount = 0;
1608 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001609
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001610 // First look for preindexed address forms, that is after the "[Rn" we now
1611 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001612 if (Tok.is(AsmToken::Comma)) {
1613 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001614 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001615
Chris Lattner550276e2010-10-28 20:52:15 +00001616 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1617 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001618 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001619 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001620 if (RBracTok.isNot(AsmToken::RBrac)) {
1621 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001622 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001623 }
Sean Callanan76264762010-04-02 22:27:05 +00001624 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001625 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001626
Sean Callanan18b83232010-01-19 21:44:56 +00001627 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001628 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001629 // None of addrmode3 instruction uses "!"
1630 if (AddrMode == ARMII::AddrMode3)
1631 return true;
1632
Bill Wendling50d0f582010-11-18 23:43:05 +00001633 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1634 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001635 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001636 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001637 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1638 if (AddrMode == ARMII::AddrMode2)
1639 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001640 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001641 } else {
1642 // The "[Rn" we have so far was not followed by a comma.
1643
Jim Grosbach80eb2332010-10-29 17:41:25 +00001644 // If there's anything other than the right brace, this is a post indexing
1645 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001646 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001647 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001648
Sean Callanan18b83232010-01-19 21:44:56 +00001649 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001650
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001651 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001652 Postindexed = true;
1653 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001654
Chris Lattner550276e2010-10-28 20:52:15 +00001655 if (NextTok.isNot(AsmToken::Comma)) {
1656 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001657 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001658 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001659
Sean Callananb9a25b72010-01-19 20:27:46 +00001660 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001661
Chris Lattner550276e2010-10-28 20:52:15 +00001662 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001663 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001664 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001665 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001666 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001667 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001668
1669 // Force Offset to exist if used.
1670 if (!OffsetIsReg) {
1671 if (!Offset)
1672 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001673 } else {
1674 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1675 Error(E, "shift amount not supported");
1676 return true;
1677 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001678 }
1679
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001680 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1681 Offset, OffsetRegNum, OffsetRegShifted,
1682 ShiftType, ShiftAmount, Preindexed,
1683 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001684 if (WBOp)
1685 Operands.push_back(WBOp);
1686
1687 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001688}
1689
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001690/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1691/// we will parse the following (were +/- means that a plus or minus is
1692/// optional):
1693/// +/-Rm
1694/// +/-Rm, shift
1695/// #offset
1696/// we return false on success or an error otherwise.
1697bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001698 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001699 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001700 const MCExpr *&ShiftAmount,
1701 const MCExpr *&Offset,
1702 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001703 int &OffsetRegNum,
1704 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001705 Negative = false;
1706 OffsetRegShifted = false;
1707 OffsetIsReg = false;
1708 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001709 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001710 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001711 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001712 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001713 else if (NextTok.is(AsmToken::Minus)) {
1714 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001715 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001716 }
1717 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001718 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001719 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001720 SMLoc CurLoc = OffsetRegTok.getLoc();
1721 OffsetRegNum = TryParseRegister();
1722 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001723 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001724 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001725 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001726 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001727
Bill Wendling12f40e92010-11-06 10:51:53 +00001728 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001729 if (OffsetRegNum != -1) {
1730 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001731 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001732 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001733 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001734
Sean Callanan18b83232010-01-19 21:44:56 +00001735 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001736 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001737 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001738 OffsetRegShifted = true;
1739 }
1740 }
1741 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1742 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001743 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001744 if (HashTok.isNot(AsmToken::Hash))
1745 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001746
Sean Callananb9a25b72010-01-19 20:27:46 +00001747 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001748
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001749 if (getParser().ParseExpression(Offset))
1750 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001751 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001752 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001753 return false;
1754}
1755
1756/// ParseShift as one of these two:
1757/// ( lsl | lsr | asr | ror ) , # shift_amount
1758/// rrx
1759/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001760bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1761 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001762 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001763 if (Tok.isNot(AsmToken::Identifier))
1764 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001765 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001766 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001767 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001768 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001769 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001770 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001771 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001772 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001773 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001774 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001775 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001776 else
1777 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001778 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001779
1780 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001781 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001782 return false;
1783
1784 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001785 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001786 if (HashTok.isNot(AsmToken::Hash))
1787 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001788 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001789
1790 if (getParser().ParseExpression(ShiftAmount))
1791 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001792
1793 return false;
1794}
1795
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001796/// Parse a arm instruction operand. For now this parses the operand regardless
1797/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001798bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001799 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001800 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001801
1802 // Check if the current operand has a custom associated parser, if so, try to
1803 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001804 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1805 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001806 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001807 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1808 // there was a match, but an error occurred, in which case, just return that
1809 // the operand parsing failed.
1810 if (ResTy == MatchOperand_ParseFail)
1811 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001812
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001813 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001814 default:
1815 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001816 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001817 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001818 if (!TryParseRegisterWithWriteBack(Operands))
1819 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001820 int Res = TryParseShiftRegister(Operands);
1821 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001822 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001823 else if (Res == -1) // irrecoverable error
1824 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001825
1826 // Fall though for the Identifier case that is not a register or a
1827 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00001828 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00001829 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1830 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001831 // This was not a register so parse other operands that start with an
1832 // identifier (like labels) as expressions and create them as immediates.
1833 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001834 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001835 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001836 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001837 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001838 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1839 return false;
1840 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001841 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001842 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001843 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001844 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001845 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001846 // #42 -> immediate.
1847 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001848 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001849 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001850 const MCExpr *ImmVal;
1851 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001852 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001853 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001854 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1855 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001856 case AsmToken::Colon: {
1857 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001858 // FIXME: Check it's an expression prefix,
1859 // e.g. (FOO - :lower16:BAR) isn't legal.
1860 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001861 if (ParsePrefix(RefKind))
1862 return true;
1863
Evan Cheng75972122011-01-13 07:58:56 +00001864 const MCExpr *SubExprVal;
1865 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001866 return true;
1867
Evan Cheng75972122011-01-13 07:58:56 +00001868 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1869 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001870 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001871 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001872 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001873 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001874 }
1875}
1876
Evan Cheng75972122011-01-13 07:58:56 +00001877// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1878// :lower16: and :upper16:.
1879bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1880 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001881
1882 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001883 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001884 Parser.Lex(); // Eat ':'
1885
1886 if (getLexer().isNot(AsmToken::Identifier)) {
1887 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1888 return true;
1889 }
1890
1891 StringRef IDVal = Parser.getTok().getIdentifier();
1892 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001893 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001894 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001895 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001896 } else {
1897 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1898 return true;
1899 }
1900 Parser.Lex();
1901
1902 if (getLexer().isNot(AsmToken::Colon)) {
1903 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1904 return true;
1905 }
1906 Parser.Lex(); // Eat the last ':'
1907 return false;
1908}
1909
1910const MCExpr *
1911ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1912 MCSymbolRefExpr::VariantKind Variant) {
1913 // Recurse over the given expression, rebuilding it to apply the given variant
1914 // to the leftmost symbol.
1915 if (Variant == MCSymbolRefExpr::VK_None)
1916 return E;
1917
1918 switch (E->getKind()) {
1919 case MCExpr::Target:
1920 llvm_unreachable("Can't handle target expr yet");
1921 case MCExpr::Constant:
1922 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1923
1924 case MCExpr::SymbolRef: {
1925 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1926
1927 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1928 return 0;
1929
1930 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1931 }
1932
1933 case MCExpr::Unary:
1934 llvm_unreachable("Can't handle unary expressions yet");
1935
1936 case MCExpr::Binary: {
1937 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1938 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1939 const MCExpr *RHS = BE->getRHS();
1940 if (!LHS)
1941 return 0;
1942
1943 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1944 }
1945 }
1946
1947 assert(0 && "Invalid expression kind!");
1948 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001949}
1950
Daniel Dunbar352e1482011-01-11 15:59:50 +00001951/// \brief Given a mnemonic, split out possible predication code and carry
1952/// setting letters to form a canonical mnemonic and flags.
1953//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001954// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001955static StringRef SplitMnemonic(StringRef Mnemonic,
1956 unsigned &PredicationCode,
1957 bool &CarrySetting,
1958 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001959 PredicationCode = ARMCC::AL;
1960 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001961 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001962
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001963 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001964 //
1965 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001966 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1967 Mnemonic == "movs" ||
1968 Mnemonic == "svc" ||
1969 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1970 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1971 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1972 Mnemonic == "vclt" ||
1973 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1974 Mnemonic == "vcle" ||
1975 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1976 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbach5a187002011-07-19 18:32:48 +00001977 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001978 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001979
Jim Grosbach3f00e312011-07-11 17:09:57 +00001980 // First, split out any predication code. Ignore mnemonics we know aren't
1981 // predicated but do have a carry-set and so weren't caught above.
Jim Grosbach5a187002011-07-19 18:32:48 +00001982 if (Mnemonic != "adcs" && Mnemonic != "bics") {
Jim Grosbach3f00e312011-07-11 17:09:57 +00001983 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1984 .Case("eq", ARMCC::EQ)
1985 .Case("ne", ARMCC::NE)
1986 .Case("hs", ARMCC::HS)
1987 .Case("cs", ARMCC::HS)
1988 .Case("lo", ARMCC::LO)
1989 .Case("cc", ARMCC::LO)
1990 .Case("mi", ARMCC::MI)
1991 .Case("pl", ARMCC::PL)
1992 .Case("vs", ARMCC::VS)
1993 .Case("vc", ARMCC::VC)
1994 .Case("hi", ARMCC::HI)
1995 .Case("ls", ARMCC::LS)
1996 .Case("ge", ARMCC::GE)
1997 .Case("lt", ARMCC::LT)
1998 .Case("gt", ARMCC::GT)
1999 .Case("le", ARMCC::LE)
2000 .Case("al", ARMCC::AL)
2001 .Default(~0U);
2002 if (CC != ~0U) {
2003 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
2004 PredicationCode = CC;
2005 }
Bill Wendling52925b62010-10-29 23:50:21 +00002006 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002007
Daniel Dunbar352e1482011-01-11 15:59:50 +00002008 // Next, determine if we have a carry setting bit. We explicitly ignore all
2009 // the instructions we know end in 's'.
2010 if (Mnemonic.endswith("s") &&
2011 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
2012 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
2013 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
2014 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
2015 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
2016 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
2017 CarrySetting = true;
2018 }
2019
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002020 // The "cps" instruction can have a interrupt mode operand which is glued into
2021 // the mnemonic. Check if this is the case, split it and parse the imod op
2022 if (Mnemonic.startswith("cps")) {
2023 // Split out any imod code.
2024 unsigned IMod =
2025 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
2026 .Case("ie", ARM_PROC::IE)
2027 .Case("id", ARM_PROC::ID)
2028 .Default(~0U);
2029 if (IMod != ~0U) {
2030 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2031 ProcessorIMod = IMod;
2032 }
2033 }
2034
Daniel Dunbar352e1482011-01-11 15:59:50 +00002035 return Mnemonic;
2036}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002037
2038/// \brief Given a canonical mnemonic, determine if the instruction ever allows
2039/// inclusion of carry set or predication code operands.
2040//
2041// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00002042void ARMAsmParser::
2043GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2044 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002045 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2046 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2047 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2048 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002049 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002050 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2051 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002052 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00002053 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002054 CanAcceptCarrySet = true;
2055 } else {
2056 CanAcceptCarrySet = false;
2057 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002058
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002059 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2060 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2061 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2062 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00002063 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002064 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002065 CanAcceptPredicationCode = false;
2066 } else {
2067 CanAcceptPredicationCode = true;
2068 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002069
Evan Chengebdeeab2011-07-08 01:53:10 +00002070 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002071 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002072 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002073 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002074}
2075
2076/// Parse an arm instruction mnemonic followed by its operands.
2077bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2078 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2079 // Create the leading tokens for the mnemonic, split by '.' characters.
2080 size_t Start = 0, Next = Name.find('.');
Jim Grosbachffa32252011-07-19 19:13:28 +00002081 StringRef Mnemonic = Name.slice(Start, Next);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002082
Daniel Dunbar352e1482011-01-11 15:59:50 +00002083 // Split out the predication code and carry setting flag from the mnemonic.
2084 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002085 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002086 bool CarrySetting;
Jim Grosbachffa32252011-07-19 19:13:28 +00002087 Mnemonic = SplitMnemonic(Mnemonic, PredicationCode, CarrySetting,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002088 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002089
Jim Grosbachffa32252011-07-19 19:13:28 +00002090 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
2091
2092 // FIXME: This is all a pretty gross hack. We should automatically handle
2093 // optional operands like this via tblgen.
Bill Wendling9717fa92010-11-21 10:56:05 +00002094
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002095 // Next, add the CCOut and ConditionCode operands, if needed.
2096 //
2097 // For mnemonics which can ever incorporate a carry setting bit or predication
2098 // code, our matching model involves us always generating CCOut and
2099 // ConditionCode operands to match the mnemonic "as written" and then we let
2100 // the matcher deal with finding the right instruction or generating an
2101 // appropriate error.
2102 bool CanAcceptCarrySet, CanAcceptPredicationCode;
Jim Grosbachffa32252011-07-19 19:13:28 +00002103 GetMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002104
Jim Grosbach33c16a22011-07-14 22:04:21 +00002105 // If we had a carry-set on an instruction that can't do that, issue an
2106 // error.
2107 if (!CanAcceptCarrySet && CarrySetting) {
2108 Parser.EatToEndOfStatement();
Jim Grosbachffa32252011-07-19 19:13:28 +00002109 return Error(NameLoc, "instruction '" + Mnemonic +
Jim Grosbach33c16a22011-07-14 22:04:21 +00002110 "' can not set flags, but 's' suffix specified");
2111 }
2112
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002113 // Add the carry setting operand, if necessary.
2114 //
2115 // FIXME: It would be awesome if we could somehow invent a location such that
2116 // match errors on this operand would print a nice diagnostic about how the
2117 // 's' character in the mnemonic resulted in a CCOut operand.
Jim Grosbach33c16a22011-07-14 22:04:21 +00002118 if (CanAcceptCarrySet)
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002119 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2120 NameLoc));
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002121
2122 // Add the predication code operand, if necessary.
2123 if (CanAcceptPredicationCode) {
2124 Operands.push_back(ARMOperand::CreateCondCode(
2125 ARMCC::CondCodes(PredicationCode), NameLoc));
2126 } else {
2127 // This mnemonic can't ever accept a predication code, but the user wrote
2128 // one (or misspelled another mnemonic).
2129
2130 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002131 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002132
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002133 // Add the processor imod operand, if necessary.
2134 if (ProcessorIMod) {
2135 Operands.push_back(ARMOperand::CreateImm(
2136 MCConstantExpr::Create(ProcessorIMod, getContext()),
2137 NameLoc, NameLoc));
2138 } else {
2139 // This mnemonic can't ever accept a imod, but the user wrote
2140 // one (or misspelled another mnemonic).
2141
2142 // FIXME: Issue a nice error.
2143 }
2144
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002145 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002146 while (Next != StringRef::npos) {
2147 Start = Next;
2148 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002149 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002150
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002151 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002152 }
2153
2154 // Read the remaining operands.
2155 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002156 // Read the first operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002157 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002158 Parser.EatToEndOfStatement();
2159 return true;
2160 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002161
2162 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002163 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002164
2165 // Parse and remember the operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002166 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002167 Parser.EatToEndOfStatement();
2168 return true;
2169 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002170 }
2171 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002172
Chris Lattnercbf8a982010-09-11 16:18:25 +00002173 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2174 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002175 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002176 }
Bill Wendling146018f2010-11-06 21:42:12 +00002177
Chris Lattner34e53142010-09-08 05:10:46 +00002178 Parser.Lex(); // Consume the EndOfStatement
Jim Grosbachffa32252011-07-19 19:13:28 +00002179
2180
2181 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
2182 // another does not. Specifically, the MOVW instruction does not. So we
2183 // special case it here and remove the defaulted (non-setting) cc_out
2184 // operand if that's the instruction we're trying to match.
2185 //
2186 // We do this post-processing of the explicit operands rather than just
2187 // conditionally adding the cc_out in the first place because we need
2188 // to check the type of the parsed immediate operand.
2189 if (Mnemonic == "mov" && Operands.size() > 4 &&
2190 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
Jim Grosbach731f2092011-07-19 19:45:44 +00002191 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
2192 static_cast<ARMOperand*>(Operands[1])->getReg() == 0) {
Jim Grosbachffa32252011-07-19 19:13:28 +00002193 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
2194 Operands.erase(Operands.begin() + 1);
2195 delete Op;
2196 }
2197
2198
2199
Chris Lattner98986712010-01-14 22:21:20 +00002200 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002201}
2202
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002203bool ARMAsmParser::
2204MatchAndEmitInstruction(SMLoc IDLoc,
2205 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2206 MCStreamer &Out) {
2207 MCInst Inst;
2208 unsigned ErrorInfo;
Jim Grosbach5a187002011-07-19 18:32:48 +00002209 MatchResultTy MatchResult;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002210 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002211 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002212 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002213 Out.EmitInstruction(Inst);
2214 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002215 case Match_MissingFeature:
2216 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2217 return true;
2218 case Match_InvalidOperand: {
2219 SMLoc ErrorLoc = IDLoc;
2220 if (ErrorInfo != ~0U) {
2221 if (ErrorInfo >= Operands.size())
2222 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002223
Chris Lattnere73d4f82010-10-28 21:41:58 +00002224 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2225 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2226 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002227
Chris Lattnere73d4f82010-10-28 21:41:58 +00002228 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002229 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002230 case Match_MnemonicFail:
2231 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002232 case Match_ConversionFail:
2233 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002234 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002235
Eric Christopherc223e2b2010-10-29 09:26:59 +00002236 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002237 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002238}
2239
Kevin Enderby515d5092009-10-15 20:48:48 +00002240/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002241bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2242 StringRef IDVal = DirectiveID.getIdentifier();
2243 if (IDVal == ".word")
2244 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002245 else if (IDVal == ".thumb")
2246 return ParseDirectiveThumb(DirectiveID.getLoc());
2247 else if (IDVal == ".thumb_func")
2248 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2249 else if (IDVal == ".code")
2250 return ParseDirectiveCode(DirectiveID.getLoc());
2251 else if (IDVal == ".syntax")
2252 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002253 return true;
2254}
2255
2256/// ParseDirectiveWord
2257/// ::= .word [ expression (, expression)* ]
2258bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2259 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2260 for (;;) {
2261 const MCExpr *Value;
2262 if (getParser().ParseExpression(Value))
2263 return true;
2264
Chris Lattneraaec2052010-01-19 19:46:13 +00002265 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002266
2267 if (getLexer().is(AsmToken::EndOfStatement))
2268 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002269
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002270 // FIXME: Improve diagnostic.
2271 if (getLexer().isNot(AsmToken::Comma))
2272 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002273 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002274 }
2275 }
2276
Sean Callananb9a25b72010-01-19 20:27:46 +00002277 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002278 return false;
2279}
2280
Kevin Enderby515d5092009-10-15 20:48:48 +00002281/// ParseDirectiveThumb
2282/// ::= .thumb
2283bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2284 if (getLexer().isNot(AsmToken::EndOfStatement))
2285 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002286 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002287
2288 // TODO: set thumb mode
2289 // TODO: tell the MC streamer the mode
2290 // getParser().getStreamer().Emit???();
2291 return false;
2292}
2293
2294/// ParseDirectiveThumbFunc
2295/// ::= .thumbfunc symbol_name
2296bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002297 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2298 bool isMachO = MAI.hasSubsectionsViaSymbols();
2299 StringRef Name;
2300
2301 // Darwin asm has function name after .thumb_func direction
2302 // ELF doesn't
2303 if (isMachO) {
2304 const AsmToken &Tok = Parser.getTok();
2305 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2306 return Error(L, "unexpected token in .thumb_func directive");
2307 Name = Tok.getString();
2308 Parser.Lex(); // Consume the identifier token.
2309 }
2310
Kevin Enderby515d5092009-10-15 20:48:48 +00002311 if (getLexer().isNot(AsmToken::EndOfStatement))
2312 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002313 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002314
Rafael Espindola64695402011-05-16 16:17:21 +00002315 // FIXME: assuming function name will be the line following .thumb_func
2316 if (!isMachO) {
2317 Name = Parser.getTok().getString();
2318 }
2319
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002320 // Mark symbol as a thumb symbol.
2321 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2322 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002323 return false;
2324}
2325
2326/// ParseDirectiveSyntax
2327/// ::= .syntax unified | divided
2328bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002329 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002330 if (Tok.isNot(AsmToken::Identifier))
2331 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002332 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002333 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002334 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002335 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002336 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002337 else
2338 return Error(L, "unrecognized syntax mode in .syntax directive");
2339
2340 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002341 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002342 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002343
2344 // TODO tell the MC streamer the mode
2345 // getParser().getStreamer().Emit???();
2346 return false;
2347}
2348
2349/// ParseDirectiveCode
2350/// ::= .code 16 | 32
2351bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002352 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002353 if (Tok.isNot(AsmToken::Integer))
2354 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002355 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002356 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002357 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002358 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002359 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002360 else
2361 return Error(L, "invalid operand to .code directive");
2362
2363 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002364 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002365 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002366
Evan Cheng32869202011-07-08 22:36:29 +00002367 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002368 if (!isThumb())
2369 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002370 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002371 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002372 if (isThumb())
2373 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002374 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002375 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002376
Kevin Enderby515d5092009-10-15 20:48:48 +00002377 return false;
2378}
2379
Sean Callanan90b70972010-04-07 20:29:34 +00002380extern "C" void LLVMInitializeARMAsmLexer();
2381
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002382/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002383extern "C" void LLVMInitializeARMAsmParser() {
2384 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2385 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002386 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002387}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002388
Chris Lattner0692ee62010-09-06 19:11:01 +00002389#define GET_REGISTER_MATCHER
2390#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002391#include "ARMGenAsmMatcher.inc"