blob: c72b206db9e359072e264d8a35a08b8e1e36b794 [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
34#define GET_SUBTARGETINFO_ENUM
35#include "ARMGenSubtargetInfo.inc"
36
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000037using namespace llvm;
38
Chris Lattner3a697562010-10-28 17:20:03 +000039namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000040
41class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000042
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000043class ARMAsmParser : public TargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000044 MCSubtargetInfo &STI;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045 MCAsmParser &Parser;
46
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000047 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
49
50 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000051 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
52
Chris Lattnere5658fa2010-10-30 04:09:10 +000053 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000054 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000055 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach19906722011-07-13 18:49:30 +000056 int TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000057 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000058 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
59 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000060 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000061 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000062 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
63 MCSymbolRefExpr::VariantKind Variant);
64
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000065
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 bool ParseMemoryOffsetReg(bool &Negative,
67 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000068 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000069 const MCExpr *&ShiftAmount,
70 const MCExpr *&Offset,
71 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000072 int &OffsetRegNum,
73 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000074 bool ParseShift(enum ARM_AM::ShiftOpc &St,
75 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000076 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000078 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000080 bool ParseDirectiveSyntax(SMLoc L);
81
Chris Lattner7036f8b2010-09-29 01:42:58 +000082 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000083 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000084 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000085 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
86 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000087
Evan Chengebdeeab2011-07-08 01:53:10 +000088 bool isThumb() const {
89 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000090 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000091 }
Evan Chengebdeeab2011-07-08 01:53:10 +000092 bool isThumbOne() const {
Evan Chengffc0e732011-07-09 05:47:46 +000093 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000094 }
Evan Cheng32869202011-07-08 22:36:29 +000095 void SwitchMode() {
Evan Chengffc0e732011-07-09 05:47:46 +000096 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
97 setAvailableFeatures(FB);
Evan Cheng32869202011-07-08 22:36:29 +000098 }
Evan Chengebdeeab2011-07-08 01:53:10 +000099
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000100 /// @name Auto-generated Match Functions
101 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000102
Chris Lattner0692ee62010-09-06 19:11:01 +0000103#define GET_ASSEMBLER_HEADER
104#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000105
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000106 /// }
107
Jim Grosbachf922c472011-02-12 01:34:40 +0000108 OperandMatchResultTy tryParseCoprocNumOperand(
109 SmallVectorImpl<MCParsedAsmOperand*>&);
110 OperandMatchResultTy tryParseCoprocRegOperand(
111 SmallVectorImpl<MCParsedAsmOperand*>&);
112 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000113 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000114 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000115 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000116 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000117 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000118 OperandMatchResultTy tryParseMemMode2Operand(
119 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000120 OperandMatchResultTy tryParseMemMode3Operand(
121 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000122
123 // Asm Match Converter Methods
124 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
125 const SmallVectorImpl<MCParsedAsmOperand*> &);
126 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
127 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000128 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
129 const SmallVectorImpl<MCParsedAsmOperand*> &);
130 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
131 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000132
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000133public:
Evan Chengffc0e732011-07-09 05:47:46 +0000134 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
135 : TargetAsmParser(), STI(_STI), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000136 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000137
Evan Chengebdeeab2011-07-08 01:53:10 +0000138 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000139 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evan Chengebdeeab2011-07-08 01:53:10 +0000140 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000141
Benjamin Kramer38e59892010-07-14 22:38:02 +0000142 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000143 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000144 virtual bool ParseDirective(AsmToken DirectiveID);
145};
Jim Grosbach16c74252010-10-29 14:46:02 +0000146} // end anonymous namespace
147
Chris Lattner3a697562010-10-28 17:20:03 +0000148namespace {
149
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000150/// ARMOperand - Instances of this class represent a parsed ARM machine
151/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000152class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000153 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000154 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000155 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000156 CoprocNum,
157 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000158 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000159 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000160 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000161 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000162 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000163 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000164 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000165 DPRRegisterList,
166 SPRRegisterList,
Jim Grosbache8606dc2011-07-13 17:50:29 +0000167 ShiftedRegister,
Owen Anderson00828302011-03-18 22:50:18 +0000168 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000169 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000170 } Kind;
171
Sean Callanan76264762010-04-02 22:27:05 +0000172 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000173 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000174
175 union {
176 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000177 ARMCC::CondCodes Val;
178 } CC;
179
180 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000181 ARM_MB::MemBOpt Val;
182 } MBOpt;
183
184 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000185 unsigned Val;
186 } Cop;
187
188 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000189 ARM_PROC::IFlags Val;
190 } IFlags;
191
192 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000193 unsigned Val;
194 } MMask;
195
196 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000197 const char *Data;
198 unsigned Length;
199 } Tok;
200
201 struct {
202 unsigned RegNum;
203 } Reg;
204
Bill Wendling8155e5b2010-11-06 22:19:43 +0000205 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000206 const MCExpr *Val;
207 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000208
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000209 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000210 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000211 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000212 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000213 union {
214 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
215 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
216 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000217 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000218 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000219 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000220 unsigned Preindexed : 1;
221 unsigned Postindexed : 1;
222 unsigned OffsetIsReg : 1;
223 unsigned Negative : 1; // only used when OffsetIsReg is true
224 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000225 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000226
227 struct {
228 ARM_AM::ShiftOpc ShiftTy;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000229 unsigned Imm;
Owen Anderson00828302011-03-18 22:50:18 +0000230 } Shift;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000231 struct {
232 ARM_AM::ShiftOpc ShiftTy;
233 unsigned SrcReg;
234 unsigned ShiftReg;
235 unsigned ShiftImm;
236 } ShiftedReg;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000237 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000238
Bill Wendling146018f2010-11-06 21:42:12 +0000239 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
240public:
Sean Callanan76264762010-04-02 22:27:05 +0000241 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
242 Kind = o.Kind;
243 StartLoc = o.StartLoc;
244 EndLoc = o.EndLoc;
245 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000246 case CondCode:
247 CC = o.CC;
248 break;
Sean Callanan76264762010-04-02 22:27:05 +0000249 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000250 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000251 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000252 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000253 case Register:
254 Reg = o.Reg;
255 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000256 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000257 case DPRRegisterList:
258 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000259 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000260 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000261 case CoprocNum:
262 case CoprocReg:
263 Cop = o.Cop;
264 break;
Sean Callanan76264762010-04-02 22:27:05 +0000265 case Immediate:
266 Imm = o.Imm;
267 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000268 case MemBarrierOpt:
269 MBOpt = o.MBOpt;
270 break;
Sean Callanan76264762010-04-02 22:27:05 +0000271 case Memory:
272 Mem = o.Mem;
273 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000274 case MSRMask:
275 MMask = o.MMask;
276 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000277 case ProcIFlags:
278 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000279 break;
280 case Shifter:
281 Shift = o.Shift;
282 break;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000283 case ShiftedRegister:
284 ShiftedReg = o.ShiftedReg;
285 break;
Sean Callanan76264762010-04-02 22:27:05 +0000286 }
287 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000288
Sean Callanan76264762010-04-02 22:27:05 +0000289 /// getStartLoc - Get the location of the first token of this operand.
290 SMLoc getStartLoc() const { return StartLoc; }
291 /// getEndLoc - Get the location of the last token of this operand.
292 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000293
Daniel Dunbar8462b302010-08-11 06:36:53 +0000294 ARMCC::CondCodes getCondCode() const {
295 assert(Kind == CondCode && "Invalid access!");
296 return CC.Val;
297 }
298
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000299 unsigned getCoproc() const {
300 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
301 return Cop.Val;
302 }
303
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000304 StringRef getToken() const {
305 assert(Kind == Token && "Invalid access!");
306 return StringRef(Tok.Data, Tok.Length);
307 }
308
309 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000310 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000311 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000312 }
313
Bill Wendling5fa22a12010-11-09 23:28:44 +0000314 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000315 assert((Kind == RegisterList || Kind == DPRRegisterList ||
316 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000317 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000318 }
319
Kevin Enderbycfe07242009-10-13 22:19:02 +0000320 const MCExpr *getImm() const {
321 assert(Kind == Immediate && "Invalid access!");
322 return Imm.Val;
323 }
324
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000325 ARM_MB::MemBOpt getMemBarrierOpt() const {
326 assert(Kind == MemBarrierOpt && "Invalid access!");
327 return MBOpt.Val;
328 }
329
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000330 ARM_PROC::IFlags getProcIFlags() const {
331 assert(Kind == ProcIFlags && "Invalid access!");
332 return IFlags.Val;
333 }
334
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000335 unsigned getMSRMask() const {
336 assert(Kind == MSRMask && "Invalid access!");
337 return MMask.Val;
338 }
339
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000340 /// @name Memory Operand Accessors
341 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000342 ARMII::AddrMode getMemAddrMode() const {
343 return Mem.AddrMode;
344 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000345 unsigned getMemBaseRegNum() const {
346 return Mem.BaseRegNum;
347 }
348 unsigned getMemOffsetRegNum() const {
349 assert(Mem.OffsetIsReg && "Invalid access!");
350 return Mem.Offset.RegNum;
351 }
352 const MCExpr *getMemOffset() const {
353 assert(!Mem.OffsetIsReg && "Invalid access!");
354 return Mem.Offset.Value;
355 }
356 unsigned getMemOffsetRegShifted() const {
357 assert(Mem.OffsetIsReg && "Invalid access!");
358 return Mem.OffsetRegShifted;
359 }
360 const MCExpr *getMemShiftAmount() const {
361 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
362 return Mem.ShiftAmount;
363 }
Owen Anderson00828302011-03-18 22:50:18 +0000364 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000365 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
366 return Mem.ShiftType;
367 }
368 bool getMemPreindexed() const { return Mem.Preindexed; }
369 bool getMemPostindexed() const { return Mem.Postindexed; }
370 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
371 bool getMemNegative() const { return Mem.Negative; }
372 bool getMemWriteback() const { return Mem.Writeback; }
373
374 /// @}
375
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000376 bool isCoprocNum() const { return Kind == CoprocNum; }
377 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000378 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000379 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000380 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000381 bool isImm0_255() const {
382 if (Kind != Immediate)
383 return false;
384 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
385 if (!CE) return false;
386 int64_t Value = CE->getValue();
387 return Value >= 0 && Value < 256;
388 }
389 bool isT2SOImm() const {
390 if (Kind != Immediate)
391 return false;
392 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
393 if (!CE) return false;
394 int64_t Value = CE->getValue();
395 return ARM_AM::getT2SOImmVal(Value) != -1;
396 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000397 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000398 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000399 bool isDPRRegList() const { return Kind == DPRRegisterList; }
400 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000401 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000402 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000403 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000404 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000405 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000406 bool isMemMode2() const {
407 if (getMemAddrMode() != ARMII::AddrMode2)
408 return false;
409
410 if (getMemOffsetIsReg())
411 return true;
412
413 if (getMemNegative() &&
414 !(getMemPostindexed() || getMemPreindexed()))
415 return false;
416
417 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
418 if (!CE) return false;
419 int64_t Value = CE->getValue();
420
421 // The offset must be in the range 0-4095 (imm12).
422 if (Value > 4095 || Value < -4095)
423 return false;
424
425 return true;
426 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000427 bool isMemMode3() const {
428 if (getMemAddrMode() != ARMII::AddrMode3)
429 return false;
430
431 if (getMemOffsetIsReg()) {
432 if (getMemOffsetRegShifted())
433 return false; // No shift with offset reg allowed
434 return true;
435 }
436
437 if (getMemNegative() &&
438 !(getMemPostindexed() || getMemPreindexed()))
439 return false;
440
441 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
442 if (!CE) return false;
443 int64_t Value = CE->getValue();
444
445 // The offset must be in the range 0-255 (imm8).
446 if (Value > 255 || Value < -255)
447 return false;
448
449 return true;
450 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000451 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000452 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
453 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000454 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000455
Daniel Dunbar4b462672011-01-18 05:55:27 +0000456 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000457 if (!CE) return false;
458
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000459 // The offset must be a multiple of 4 in the range 0-1020.
460 int64_t Value = CE->getValue();
461 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
462 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000463 bool isMemMode7() const {
464 if (!isMemory() ||
465 getMemPreindexed() ||
466 getMemPostindexed() ||
467 getMemOffsetIsReg() ||
468 getMemNegative() ||
469 getMemWriteback())
470 return false;
471
472 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
473 if (!CE) return false;
474
475 if (CE->getValue())
476 return false;
477
478 return true;
479 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000480 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000481 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000482 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000483 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000484 }
485 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000486 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000487 return false;
488
Daniel Dunbar4b462672011-01-18 05:55:27 +0000489 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000490 if (!CE) return false;
491
492 // The offset must be a multiple of 4 in the range 0-124.
493 uint64_t Value = CE->getValue();
494 return ((Value & 0x3) == 0 && Value <= 124);
495 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000496 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000497 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000498
499 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000500 // Add as immediates when possible. Null MCExpr = 0.
501 if (Expr == 0)
502 Inst.addOperand(MCOperand::CreateImm(0));
503 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000504 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
505 else
506 Inst.addOperand(MCOperand::CreateExpr(Expr));
507 }
508
Daniel Dunbar8462b302010-08-11 06:36:53 +0000509 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000510 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000511 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000512 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
513 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000514 }
515
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000516 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
517 assert(N == 1 && "Invalid number of operands!");
518 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
519 }
520
521 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
522 assert(N == 1 && "Invalid number of operands!");
523 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
524 }
525
Jim Grosbachd67641b2010-12-06 18:21:12 +0000526 void addCCOutOperands(MCInst &Inst, unsigned N) const {
527 assert(N == 1 && "Invalid number of operands!");
528 Inst.addOperand(MCOperand::CreateReg(getReg()));
529 }
530
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000531 void addRegOperands(MCInst &Inst, unsigned N) const {
532 assert(N == 1 && "Invalid number of operands!");
533 Inst.addOperand(MCOperand::CreateReg(getReg()));
534 }
535
Jim Grosbache8606dc2011-07-13 17:50:29 +0000536 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
537 assert(N == 3 && "Invalid number of operands!");
538 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
539 assert((ShiftedReg.ShiftReg == 0 ||
540 ARM_AM::getSORegOffset(ShiftedReg.ShiftImm) == 0) &&
541 "Invalid shifted register operand!");
542 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
543 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
544 Inst.addOperand(MCOperand::CreateImm(
545 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
546 }
547
Owen Anderson00828302011-03-18 22:50:18 +0000548 void addShifterOperands(MCInst &Inst, unsigned N) const {
549 assert(N == 1 && "Invalid number of operands!");
550 Inst.addOperand(MCOperand::CreateImm(
551 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
552 }
553
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000554 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000555 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000556 const SmallVectorImpl<unsigned> &RegList = getRegList();
557 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000558 I = RegList.begin(), E = RegList.end(); I != E; ++I)
559 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000560 }
561
Bill Wendling0f630752010-11-17 04:32:08 +0000562 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
563 addRegListOperands(Inst, N);
564 }
565
566 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
567 addRegListOperands(Inst, N);
568 }
569
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000570 void addImmOperands(MCInst &Inst, unsigned N) const {
571 assert(N == 1 && "Invalid number of operands!");
572 addExpr(Inst, getImm());
573 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000574
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000575 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
576 assert(N == 1 && "Invalid number of operands!");
577 addExpr(Inst, getImm());
578 }
579
580 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
581 assert(N == 1 && "Invalid number of operands!");
582 addExpr(Inst, getImm());
583 }
584
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000585 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
586 assert(N == 1 && "Invalid number of operands!");
587 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
588 }
589
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000590 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
591 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
592 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
593
594 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000595 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000596 assert((CE || CE->getValue() == 0) &&
597 "No offset operand support in mode 7");
598 }
599
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000600 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
601 assert(isMemMode2() && "Invalid mode or number of operands!");
602 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
603 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
604
605 if (getMemOffsetIsReg()) {
606 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
607
608 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
609 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
610 int64_t ShiftAmount = 0;
611
612 if (getMemOffsetRegShifted()) {
613 ShOpc = getMemShiftType();
614 const MCConstantExpr *CE =
615 dyn_cast<MCConstantExpr>(getMemShiftAmount());
616 ShiftAmount = CE->getValue();
617 }
618
619 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
620 ShOpc, IdxMode)));
621 return;
622 }
623
624 // Create a operand placeholder to always yield the same number of operands.
625 Inst.addOperand(MCOperand::CreateReg(0));
626
627 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
628 // the difference?
629 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
630 assert(CE && "Non-constant mode 2 offset operand!");
631 int64_t Offset = CE->getValue();
632
633 if (Offset >= 0)
634 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
635 Offset, ARM_AM::no_shift, IdxMode)));
636 else
637 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
638 -Offset, ARM_AM::no_shift, IdxMode)));
639 }
640
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000641 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
642 assert(isMemMode3() && "Invalid mode or number of operands!");
643 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
644 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
645
646 if (getMemOffsetIsReg()) {
647 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
648
649 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
650 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
651 IdxMode)));
652 return;
653 }
654
655 // Create a operand placeholder to always yield the same number of operands.
656 Inst.addOperand(MCOperand::CreateReg(0));
657
658 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
659 // the difference?
660 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
661 assert(CE && "Non-constant mode 3 offset operand!");
662 int64_t Offset = CE->getValue();
663
664 if (Offset >= 0)
665 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
666 Offset, IdxMode)));
667 else
668 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
669 -Offset, IdxMode)));
670 }
671
Chris Lattner14b93852010-10-29 00:27:31 +0000672 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
673 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000674
Daniel Dunbar4b462672011-01-18 05:55:27 +0000675 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
676 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000677
Jim Grosbach80eb2332010-10-29 17:41:25 +0000678 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
679 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000680 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000681 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000682
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000683 // The MCInst offset operand doesn't include the low two bits (like
684 // the instruction encoding).
685 int64_t Offset = CE->getValue() / 4;
686 if (Offset >= 0)
687 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
688 Offset)));
689 else
690 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
691 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000692 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000693
Bill Wendlingf4caf692010-12-14 03:36:38 +0000694 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
695 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000696 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
697 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000698 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000699
Bill Wendlingf4caf692010-12-14 03:36:38 +0000700 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
701 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000702 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
703 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000704 assert(CE && "Non-constant mode offset operand!");
705 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000706 }
707
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000708 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
709 assert(N == 1 && "Invalid number of operands!");
710 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
711 }
712
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000713 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
714 assert(N == 1 && "Invalid number of operands!");
715 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
716 }
717
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000718 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000719
Chris Lattner3a697562010-10-28 17:20:03 +0000720 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
721 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000722 Op->CC.Val = CC;
723 Op->StartLoc = S;
724 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000725 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000726 }
727
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000728 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
729 ARMOperand *Op = new ARMOperand(CoprocNum);
730 Op->Cop.Val = CopVal;
731 Op->StartLoc = S;
732 Op->EndLoc = S;
733 return Op;
734 }
735
736 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
737 ARMOperand *Op = new ARMOperand(CoprocReg);
738 Op->Cop.Val = CopVal;
739 Op->StartLoc = S;
740 Op->EndLoc = S;
741 return Op;
742 }
743
Jim Grosbachd67641b2010-12-06 18:21:12 +0000744 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
745 ARMOperand *Op = new ARMOperand(CCOut);
746 Op->Reg.RegNum = RegNum;
747 Op->StartLoc = S;
748 Op->EndLoc = S;
749 return Op;
750 }
751
Chris Lattner3a697562010-10-28 17:20:03 +0000752 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
753 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000754 Op->Tok.Data = Str.data();
755 Op->Tok.Length = Str.size();
756 Op->StartLoc = S;
757 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000758 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000759 }
760
Bill Wendling50d0f582010-11-18 23:43:05 +0000761 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000762 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000763 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000764 Op->StartLoc = S;
765 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000766 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000767 }
768
Jim Grosbache8606dc2011-07-13 17:50:29 +0000769 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
770 unsigned SrcReg,
771 unsigned ShiftReg,
772 unsigned ShiftImm,
773 SMLoc S, SMLoc E) {
774 ARMOperand *Op = new ARMOperand(ShiftedRegister);
775 Op->ShiftedReg.ShiftTy = ShTy;
776 Op->ShiftedReg.SrcReg = SrcReg;
777 Op->ShiftedReg.ShiftReg = ShiftReg;
778 Op->ShiftedReg.ShiftImm = ShiftImm;
779 Op->StartLoc = S;
780 Op->EndLoc = E;
781 return Op;
782 }
783
Owen Anderson00828302011-03-18 22:50:18 +0000784 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
785 SMLoc S, SMLoc E) {
786 ARMOperand *Op = new ARMOperand(Shifter);
787 Op->Shift.ShiftTy = ShTy;
788 Op->StartLoc = S;
789 Op->EndLoc = E;
790 return Op;
791 }
792
Bill Wendling7729e062010-11-09 22:44:22 +0000793 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000794 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000795 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000796 KindTy Kind = RegisterList;
797
798 if (ARM::DPRRegClass.contains(Regs.front().first))
799 Kind = DPRRegisterList;
800 else if (ARM::SPRRegClass.contains(Regs.front().first))
801 Kind = SPRRegisterList;
802
803 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000804 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000805 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000806 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000807 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000808 Op->StartLoc = StartLoc;
809 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000810 return Op;
811 }
812
Chris Lattner3a697562010-10-28 17:20:03 +0000813 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
814 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000815 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000816 Op->StartLoc = S;
817 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000818 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000819 }
820
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000821 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
822 bool OffsetIsReg, const MCExpr *Offset,
823 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000824 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000825 const MCExpr *ShiftAmount, bool Preindexed,
826 bool Postindexed, bool Negative, bool Writeback,
827 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000828 assert((OffsetRegNum == -1 || OffsetIsReg) &&
829 "OffsetRegNum must imply OffsetIsReg!");
830 assert((!OffsetRegShifted || OffsetIsReg) &&
831 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000832 assert((Offset || OffsetIsReg) &&
833 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000834 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
835 "Cannot have shift amount without shifted register offset!");
836 assert((!Offset || !OffsetIsReg) &&
837 "Cannot have expression offset and register offset!");
838
Chris Lattner3a697562010-10-28 17:20:03 +0000839 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000840 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000841 Op->Mem.BaseRegNum = BaseRegNum;
842 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000843 if (OffsetIsReg)
844 Op->Mem.Offset.RegNum = OffsetRegNum;
845 else
846 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000847 Op->Mem.OffsetRegShifted = OffsetRegShifted;
848 Op->Mem.ShiftType = ShiftType;
849 Op->Mem.ShiftAmount = ShiftAmount;
850 Op->Mem.Preindexed = Preindexed;
851 Op->Mem.Postindexed = Postindexed;
852 Op->Mem.Negative = Negative;
853 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000854
Sean Callanan76264762010-04-02 22:27:05 +0000855 Op->StartLoc = S;
856 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000857 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000858 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000859
860 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
861 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
862 Op->MBOpt.Val = Opt;
863 Op->StartLoc = S;
864 Op->EndLoc = S;
865 return Op;
866 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000867
868 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
869 ARMOperand *Op = new ARMOperand(ProcIFlags);
870 Op->IFlags.Val = IFlags;
871 Op->StartLoc = S;
872 Op->EndLoc = S;
873 return Op;
874 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000875
876 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
877 ARMOperand *Op = new ARMOperand(MSRMask);
878 Op->MMask.Val = MMask;
879 Op->StartLoc = S;
880 Op->EndLoc = S;
881 return Op;
882 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000883};
884
885} // end anonymous namespace.
886
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000887void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000888 switch (Kind) {
889 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000890 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000891 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000892 case CCOut:
893 OS << "<ccout " << getReg() << ">";
894 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000895 case CoprocNum:
896 OS << "<coprocessor number: " << getCoproc() << ">";
897 break;
898 case CoprocReg:
899 OS << "<coprocessor register: " << getCoproc() << ">";
900 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000901 case MSRMask:
902 OS << "<mask: " << getMSRMask() << ">";
903 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000904 case Immediate:
905 getImm()->print(OS);
906 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000907 case MemBarrierOpt:
908 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
909 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000910 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000911 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000912 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
913 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000914 if (getMemOffsetIsReg()) {
915 OS << " offset:<register " << getMemOffsetRegNum();
916 if (getMemOffsetRegShifted()) {
917 OS << " offset-shift-type:" << getMemShiftType();
918 OS << " offset-shift-amount:" << *getMemShiftAmount();
919 }
920 } else {
921 OS << " offset:" << *getMemOffset();
922 }
923 if (getMemOffsetIsReg())
924 OS << " (offset-is-reg)";
925 if (getMemPreindexed())
926 OS << " (pre-indexed)";
927 if (getMemPostindexed())
928 OS << " (post-indexed)";
929 if (getMemNegative())
930 OS << " (negative)";
931 if (getMemWriteback())
932 OS << " (writeback)";
933 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000934 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000935 case ProcIFlags: {
936 OS << "<ARM_PROC::";
937 unsigned IFlags = getProcIFlags();
938 for (int i=2; i >= 0; --i)
939 if (IFlags & (1 << i))
940 OS << ARM_PROC::IFlagsToString(1 << i);
941 OS << ">";
942 break;
943 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000944 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000945 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000946 break;
Owen Anderson00828302011-03-18 22:50:18 +0000947 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +0000948 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
949 break;
950 case ShiftedRegister:
951 OS << "<so_reg"
952 << ShiftedReg.SrcReg
953 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
954 << ", " << ShiftedReg.ShiftReg << ", "
955 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
956 << ">";
Owen Anderson00828302011-03-18 22:50:18 +0000957 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000958 case RegisterList:
959 case DPRRegisterList:
960 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000961 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000962
Bill Wendling5fa22a12010-11-09 23:28:44 +0000963 const SmallVectorImpl<unsigned> &RegList = getRegList();
964 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000965 I = RegList.begin(), E = RegList.end(); I != E; ) {
966 OS << *I;
967 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000968 }
969
970 OS << ">";
971 break;
972 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000973 case Token:
974 OS << "'" << getToken() << "'";
975 break;
976 }
977}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000978
979/// @name Auto-generated Match Functions
980/// {
981
982static unsigned MatchRegisterName(StringRef Name);
983
984/// }
985
Bob Wilson69df7232011-02-03 21:46:10 +0000986bool ARMAsmParser::ParseRegister(unsigned &RegNo,
987 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000988 RegNo = TryParseRegister();
989
990 return (RegNo == (unsigned)-1);
991}
992
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000993/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000994/// and if it is a register name the token is eaten and the register number is
995/// returned. Otherwise return -1.
996///
997int ARMAsmParser::TryParseRegister() {
998 const AsmToken &Tok = Parser.getTok();
999 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001000
Chris Lattnere5658fa2010-10-30 04:09:10 +00001001 // FIXME: Validate register for the current architecture; we have to do
1002 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001003 std::string upperCase = Tok.getString().str();
1004 std::string lowerCase = LowercaseString(upperCase);
1005 unsigned RegNum = MatchRegisterName(lowerCase);
1006 if (!RegNum) {
1007 RegNum = StringSwitch<unsigned>(lowerCase)
1008 .Case("r13", ARM::SP)
1009 .Case("r14", ARM::LR)
1010 .Case("r15", ARM::PC)
1011 .Case("ip", ARM::R12)
1012 .Default(0);
1013 }
1014 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001015
Chris Lattnere5658fa2010-10-30 04:09:10 +00001016 Parser.Lex(); // Eat identifier token.
1017 return RegNum;
1018}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001019
Jim Grosbach19906722011-07-13 18:49:30 +00001020// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1021// If a recoverable error occurs, return 1. If an irrecoverable error
1022// occurs, return -1. An irrecoverable error is one where tokens have been
1023// consumed in the process of trying to parse the shifter (i.e., when it is
1024// indeed a shifter operand, but malformed).
1025int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001026 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1027 SMLoc S = Parser.getTok().getLoc();
1028 const AsmToken &Tok = Parser.getTok();
1029 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1030
1031 std::string upperCase = Tok.getString().str();
1032 std::string lowerCase = LowercaseString(upperCase);
1033 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1034 .Case("lsl", ARM_AM::lsl)
1035 .Case("lsr", ARM_AM::lsr)
1036 .Case("asr", ARM_AM::asr)
1037 .Case("ror", ARM_AM::ror)
1038 .Case("rrx", ARM_AM::rrx)
1039 .Default(ARM_AM::no_shift);
1040
1041 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001042 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001043
Jim Grosbache8606dc2011-07-13 17:50:29 +00001044 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001045
Jim Grosbache8606dc2011-07-13 17:50:29 +00001046 // The source register for the shift has already been added to the
1047 // operand list, so we need to pop it off and combine it into the shifted
1048 // register operand instead.
1049 ARMOperand *PrevOp = (ARMOperand*)Operands.pop_back_val();
1050 if (!PrevOp->isReg())
1051 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1052 int SrcReg = PrevOp->getReg();
1053 int64_t Imm = 0;
1054 int ShiftReg = 0;
1055 if (ShiftTy == ARM_AM::rrx) {
1056 // RRX Doesn't have an explicit shift amount. The encoder expects
1057 // the shift register to be the same as the source register. Seems odd,
1058 // but OK.
1059 ShiftReg = SrcReg;
1060 } else {
1061 // Figure out if this is shifted by a constant or a register (for non-RRX).
1062 if (Parser.getTok().is(AsmToken::Hash)) {
1063 Parser.Lex(); // Eat hash.
1064 SMLoc ImmLoc = Parser.getTok().getLoc();
1065 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001066 if (getParser().ParseExpression(ShiftExpr)) {
1067 Error(ImmLoc, "invalid immediate shift value");
1068 return -1;
1069 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001070 // The expression must be evaluatable as an immediate.
1071 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001072 if (!CE) {
1073 Error(ImmLoc, "invalid immediate shift value");
1074 return -1;
1075 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001076 // Range check the immediate.
1077 // lsl, ror: 0 <= imm <= 31
1078 // lsr, asr: 0 <= imm <= 32
1079 Imm = CE->getValue();
1080 if (Imm < 0 ||
1081 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1082 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001083 Error(ImmLoc, "immediate shift value out of range");
1084 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001085 }
1086 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1087 ShiftReg = TryParseRegister();
1088 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001089 if (ShiftReg == -1) {
1090 Error (L, "expected immediate or register in shift operand");
1091 return -1;
1092 }
1093 } else {
1094 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001095 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001096 return -1;
1097 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001098 }
1099
Jim Grosbache8606dc2011-07-13 17:50:29 +00001100 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1101 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001102 S, Parser.getTok().getLoc()));
1103
Jim Grosbach19906722011-07-13 18:49:30 +00001104 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001105}
1106
1107
Bill Wendling50d0f582010-11-18 23:43:05 +00001108/// Try to parse a register name. The token must be an Identifier when called.
1109/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1110/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001111///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001112/// TODO this is likely to change to allow different register types and or to
1113/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001114bool ARMAsmParser::
1115TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001116 SMLoc S = Parser.getTok().getLoc();
1117 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001118 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001119 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001120
Bill Wendling50d0f582010-11-18 23:43:05 +00001121 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001122
Chris Lattnere5658fa2010-10-30 04:09:10 +00001123 const AsmToken &ExclaimTok = Parser.getTok();
1124 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001125 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1126 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001127 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001128 }
1129
Bill Wendling50d0f582010-11-18 23:43:05 +00001130 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001131}
1132
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001133/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1134/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1135/// "c5", ...
1136static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001137 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1138 // but efficient.
1139 switch (Name.size()) {
1140 default: break;
1141 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001142 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001143 return -1;
1144 switch (Name[1]) {
1145 default: return -1;
1146 case '0': return 0;
1147 case '1': return 1;
1148 case '2': return 2;
1149 case '3': return 3;
1150 case '4': return 4;
1151 case '5': return 5;
1152 case '6': return 6;
1153 case '7': return 7;
1154 case '8': return 8;
1155 case '9': return 9;
1156 }
1157 break;
1158 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001159 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001160 return -1;
1161 switch (Name[2]) {
1162 default: return -1;
1163 case '0': return 10;
1164 case '1': return 11;
1165 case '2': return 12;
1166 case '3': return 13;
1167 case '4': return 14;
1168 case '5': return 15;
1169 }
1170 break;
1171 }
1172
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001173 return -1;
1174}
1175
Jim Grosbachf922c472011-02-12 01:34:40 +00001176/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001177/// token must be an Identifier when called, and if it is a coprocessor
1178/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001179ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1180tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001181 SMLoc S = Parser.getTok().getLoc();
1182 const AsmToken &Tok = Parser.getTok();
1183 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1184
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001185 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001186 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001187 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001188
1189 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001190 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001191 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001192}
1193
Jim Grosbachf922c472011-02-12 01:34:40 +00001194/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001195/// token must be an Identifier when called, and if it is a coprocessor
1196/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001197ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1198tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001199 SMLoc S = Parser.getTok().getLoc();
1200 const AsmToken &Tok = Parser.getTok();
1201 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1202
1203 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1204 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001205 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001206
1207 Parser.Lex(); // Eat identifier token.
1208 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001209 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001210}
1211
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001212/// Parse a register list, return it if successful else return null. The first
1213/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001214bool ARMAsmParser::
1215ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001216 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001217 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001218 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001219
Bill Wendling7729e062010-11-09 22:44:22 +00001220 // Read the rest of the registers in the list.
1221 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001222 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001223
Bill Wendling7729e062010-11-09 22:44:22 +00001224 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001225 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001226 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001227
Sean Callanan18b83232010-01-19 21:44:56 +00001228 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001229 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001230 if (RegTok.isNot(AsmToken::Identifier)) {
1231 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001232 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001233 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001234
Bill Wendling1d6a2652010-11-06 10:40:24 +00001235 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001236 if (RegNum == -1) {
1237 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001238 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001239 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001240
Bill Wendlinge7176102010-11-06 22:36:58 +00001241 if (IsRange) {
1242 int Reg = PrevRegNum;
1243 do {
1244 ++Reg;
1245 Registers.push_back(std::make_pair(Reg, RegLoc));
1246 } while (Reg != RegNum);
1247 } else {
1248 Registers.push_back(std::make_pair(RegNum, RegLoc));
1249 }
1250
1251 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001252 } while (Parser.getTok().is(AsmToken::Comma) ||
1253 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001254
1255 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001256 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001257 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1258 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001259 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001260 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001261
Bill Wendlinge7176102010-11-06 22:36:58 +00001262 SMLoc E = RCurlyTok.getLoc();
1263 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001264
Bill Wendlinge7176102010-11-06 22:36:58 +00001265 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001266 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001267 RI = Registers.begin(), RE = Registers.end();
1268
Bill Wendling7caebff2011-01-12 21:20:59 +00001269 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001270 bool EmittedWarning = false;
1271
Bill Wendling7caebff2011-01-12 21:20:59 +00001272 DenseMap<unsigned, bool> RegMap;
1273 RegMap[HighRegNum] = true;
1274
Bill Wendlinge7176102010-11-06 22:36:58 +00001275 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001276 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001277 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001278
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001279 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001280 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001281 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001282 }
1283
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001284 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001285 Warning(RegInfo.second,
1286 "register not in ascending order in register list");
1287
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001288 RegMap[Reg] = true;
1289 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001290 }
1291
Bill Wendling50d0f582010-11-18 23:43:05 +00001292 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1293 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001294}
1295
Jim Grosbachf922c472011-02-12 01:34:40 +00001296/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1297ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1298tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001299 SMLoc S = Parser.getTok().getLoc();
1300 const AsmToken &Tok = Parser.getTok();
1301 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1302 StringRef OptStr = Tok.getString();
1303
1304 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1305 .Case("sy", ARM_MB::SY)
1306 .Case("st", ARM_MB::ST)
1307 .Case("ish", ARM_MB::ISH)
1308 .Case("ishst", ARM_MB::ISHST)
1309 .Case("nsh", ARM_MB::NSH)
1310 .Case("nshst", ARM_MB::NSHST)
1311 .Case("osh", ARM_MB::OSH)
1312 .Case("oshst", ARM_MB::OSHST)
1313 .Default(~0U);
1314
1315 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001316 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001317
1318 Parser.Lex(); // Eat identifier token.
1319 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001320 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001321}
1322
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001323/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001324ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1325tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1326 SMLoc S = Parser.getTok().getLoc();
1327 const AsmToken &Tok = Parser.getTok();
1328 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1329 StringRef IFlagsStr = Tok.getString();
1330
1331 unsigned IFlags = 0;
1332 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1333 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1334 .Case("a", ARM_PROC::A)
1335 .Case("i", ARM_PROC::I)
1336 .Case("f", ARM_PROC::F)
1337 .Default(~0U);
1338
1339 // If some specific iflag is already set, it means that some letter is
1340 // present more than once, this is not acceptable.
1341 if (Flag == ~0U || (IFlags & Flag))
1342 return MatchOperand_NoMatch;
1343
1344 IFlags |= Flag;
1345 }
1346
1347 Parser.Lex(); // Eat identifier token.
1348 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1349 return MatchOperand_Success;
1350}
1351
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001352/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1353ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1354tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1355 SMLoc S = Parser.getTok().getLoc();
1356 const AsmToken &Tok = Parser.getTok();
1357 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1358 StringRef Mask = Tok.getString();
1359
1360 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1361 size_t Start = 0, Next = Mask.find('_');
1362 StringRef Flags = "";
1363 StringRef SpecReg = Mask.slice(Start, Next);
1364 if (Next != StringRef::npos)
1365 Flags = Mask.slice(Next+1, Mask.size());
1366
1367 // FlagsVal contains the complete mask:
1368 // 3-0: Mask
1369 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1370 unsigned FlagsVal = 0;
1371
1372 if (SpecReg == "apsr") {
1373 FlagsVal = StringSwitch<unsigned>(Flags)
1374 .Case("nzcvq", 0x8) // same as CPSR_c
1375 .Case("g", 0x4) // same as CPSR_s
1376 .Case("nzcvqg", 0xc) // same as CPSR_fs
1377 .Default(~0U);
1378
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001379 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001380 if (!Flags.empty())
1381 return MatchOperand_NoMatch;
1382 else
1383 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001384 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001385 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001386 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1387 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001388 for (int i = 0, e = Flags.size(); i != e; ++i) {
1389 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1390 .Case("c", 1)
1391 .Case("x", 2)
1392 .Case("s", 4)
1393 .Case("f", 8)
1394 .Default(~0U);
1395
1396 // If some specific flag is already set, it means that some letter is
1397 // present more than once, this is not acceptable.
1398 if (FlagsVal == ~0U || (FlagsVal & Flag))
1399 return MatchOperand_NoMatch;
1400 FlagsVal |= Flag;
1401 }
1402 } else // No match for special register.
1403 return MatchOperand_NoMatch;
1404
1405 // Special register without flags are equivalent to "fc" flags.
1406 if (!FlagsVal)
1407 FlagsVal = 0x9;
1408
1409 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1410 if (SpecReg == "spsr")
1411 FlagsVal |= 16;
1412
1413 Parser.Lex(); // Eat identifier token.
1414 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1415 return MatchOperand_Success;
1416}
1417
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001418/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1419ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1420tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001421 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001422
1423 if (ParseMemory(Operands, ARMII::AddrMode2))
1424 return MatchOperand_NoMatch;
1425
1426 return MatchOperand_Success;
1427}
1428
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001429/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1430ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1431tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1432 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1433
1434 if (ParseMemory(Operands, ARMII::AddrMode3))
1435 return MatchOperand_NoMatch;
1436
1437 return MatchOperand_Success;
1438}
1439
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001440/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1441/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1442/// when they refer multiple MIOperands inside a single one.
1443bool ARMAsmParser::
1444CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1445 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1446 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1447
1448 // Create a writeback register dummy placeholder.
1449 Inst.addOperand(MCOperand::CreateImm(0));
1450
1451 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1452 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1453 return true;
1454}
1455
1456/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1457/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1458/// when they refer multiple MIOperands inside a single one.
1459bool ARMAsmParser::
1460CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1461 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1462 // Create a writeback register dummy placeholder.
1463 Inst.addOperand(MCOperand::CreateImm(0));
1464 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1465 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1466 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1467 return true;
1468}
1469
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001470/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1471/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1472/// when they refer multiple MIOperands inside a single one.
1473bool ARMAsmParser::
1474CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1475 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1476 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1477
1478 // Create a writeback register dummy placeholder.
1479 Inst.addOperand(MCOperand::CreateImm(0));
1480
1481 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1482 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1483 return true;
1484}
1485
1486/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1487/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1488/// when they refer multiple MIOperands inside a single one.
1489bool ARMAsmParser::
1490CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1491 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1492 // Create a writeback register dummy placeholder.
1493 Inst.addOperand(MCOperand::CreateImm(0));
1494 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1495 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1496 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1497 return true;
1498}
1499
Bill Wendlinge7176102010-11-06 22:36:58 +00001500/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001501/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001502///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001503/// TODO Only preindexing and postindexing addressing are started, unindexed
1504/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001505bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001506ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1507 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001508 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001509 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001510 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001511 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001512 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001513
Sean Callanan18b83232010-01-19 21:44:56 +00001514 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001515 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1516 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001517 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001518 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001519 int BaseRegNum = TryParseRegister();
1520 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001521 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001522 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001523 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001524
Daniel Dunbar05710932011-01-18 05:34:17 +00001525 // The next token must either be a comma or a closing bracket.
1526 const AsmToken &Tok = Parser.getTok();
1527 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1528 return true;
1529
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001530 bool Preindexed = false;
1531 bool Postindexed = false;
1532 bool OffsetIsReg = false;
1533 bool Negative = false;
1534 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001535 ARMOperand *WBOp = 0;
1536 int OffsetRegNum = -1;
1537 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001538 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001539 const MCExpr *ShiftAmount = 0;
1540 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001541
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001542 // First look for preindexed address forms, that is after the "[Rn" we now
1543 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001544 if (Tok.is(AsmToken::Comma)) {
1545 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001546 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001547
Chris Lattner550276e2010-10-28 20:52:15 +00001548 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1549 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001550 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001551 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001552 if (RBracTok.isNot(AsmToken::RBrac)) {
1553 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001554 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001555 }
Sean Callanan76264762010-04-02 22:27:05 +00001556 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001557 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001558
Sean Callanan18b83232010-01-19 21:44:56 +00001559 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001560 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001561 // None of addrmode3 instruction uses "!"
1562 if (AddrMode == ARMII::AddrMode3)
1563 return true;
1564
Bill Wendling50d0f582010-11-18 23:43:05 +00001565 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1566 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001567 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001568 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001569 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1570 if (AddrMode == ARMII::AddrMode2)
1571 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001572 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001573 } else {
1574 // The "[Rn" we have so far was not followed by a comma.
1575
Jim Grosbach80eb2332010-10-29 17:41:25 +00001576 // If there's anything other than the right brace, this is a post indexing
1577 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001578 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001579 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001580
Sean Callanan18b83232010-01-19 21:44:56 +00001581 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001582
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001583 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001584 Postindexed = true;
1585 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001586
Chris Lattner550276e2010-10-28 20:52:15 +00001587 if (NextTok.isNot(AsmToken::Comma)) {
1588 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001589 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001590 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001591
Sean Callananb9a25b72010-01-19 20:27:46 +00001592 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001593
Chris Lattner550276e2010-10-28 20:52:15 +00001594 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001595 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001596 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001597 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001598 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001599 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001600
1601 // Force Offset to exist if used.
1602 if (!OffsetIsReg) {
1603 if (!Offset)
1604 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001605 } else {
1606 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1607 Error(E, "shift amount not supported");
1608 return true;
1609 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001610 }
1611
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001612 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1613 Offset, OffsetRegNum, OffsetRegShifted,
1614 ShiftType, ShiftAmount, Preindexed,
1615 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001616 if (WBOp)
1617 Operands.push_back(WBOp);
1618
1619 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001620}
1621
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001622/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1623/// we will parse the following (were +/- means that a plus or minus is
1624/// optional):
1625/// +/-Rm
1626/// +/-Rm, shift
1627/// #offset
1628/// we return false on success or an error otherwise.
1629bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001630 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001631 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001632 const MCExpr *&ShiftAmount,
1633 const MCExpr *&Offset,
1634 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001635 int &OffsetRegNum,
1636 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001637 Negative = false;
1638 OffsetRegShifted = false;
1639 OffsetIsReg = false;
1640 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001641 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001642 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001643 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001644 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001645 else if (NextTok.is(AsmToken::Minus)) {
1646 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001647 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001648 }
1649 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001650 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001651 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001652 SMLoc CurLoc = OffsetRegTok.getLoc();
1653 OffsetRegNum = TryParseRegister();
1654 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001655 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001656 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001657 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001658 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001659
Bill Wendling12f40e92010-11-06 10:51:53 +00001660 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001661 if (OffsetRegNum != -1) {
1662 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001663 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001664 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001665 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001666
Sean Callanan18b83232010-01-19 21:44:56 +00001667 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001668 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001669 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001670 OffsetRegShifted = true;
1671 }
1672 }
1673 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1674 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001675 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001676 if (HashTok.isNot(AsmToken::Hash))
1677 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001678
Sean Callananb9a25b72010-01-19 20:27:46 +00001679 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001680
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001681 if (getParser().ParseExpression(Offset))
1682 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001683 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001684 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001685 return false;
1686}
1687
1688/// ParseShift as one of these two:
1689/// ( lsl | lsr | asr | ror ) , # shift_amount
1690/// rrx
1691/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001692bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1693 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001694 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001695 if (Tok.isNot(AsmToken::Identifier))
1696 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001697 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001698 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001699 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001700 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001701 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001702 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001703 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001704 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001705 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001706 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001707 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001708 else
1709 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001710 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001711
1712 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001713 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001714 return false;
1715
1716 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001717 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001718 if (HashTok.isNot(AsmToken::Hash))
1719 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001720 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001721
1722 if (getParser().ParseExpression(ShiftAmount))
1723 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001724
1725 return false;
1726}
1727
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001728/// Parse a arm instruction operand. For now this parses the operand regardless
1729/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001730bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001731 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001732 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001733
1734 // Check if the current operand has a custom associated parser, if so, try to
1735 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001736 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1737 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001738 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001739 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1740 // there was a match, but an error occurred, in which case, just return that
1741 // the operand parsing failed.
1742 if (ResTy == MatchOperand_ParseFail)
1743 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001744
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001745 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001746 default:
1747 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001748 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001749 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001750 if (!TryParseRegisterWithWriteBack(Operands))
1751 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001752 int Res = TryParseShiftRegister(Operands);
1753 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001754 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001755 else if (Res == -1) // irrecoverable error
1756 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001757
1758 // Fall though for the Identifier case that is not a register or a
1759 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00001760 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00001761 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1762 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001763 // This was not a register so parse other operands that start with an
1764 // identifier (like labels) as expressions and create them as immediates.
1765 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001766 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001767 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001768 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001769 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001770 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1771 return false;
1772 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001773 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001774 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001775 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001776 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001777 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001778 // #42 -> immediate.
1779 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001780 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001781 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001782 const MCExpr *ImmVal;
1783 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001784 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001785 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001786 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1787 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001788 case AsmToken::Colon: {
1789 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001790 // FIXME: Check it's an expression prefix,
1791 // e.g. (FOO - :lower16:BAR) isn't legal.
1792 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001793 if (ParsePrefix(RefKind))
1794 return true;
1795
Evan Cheng75972122011-01-13 07:58:56 +00001796 const MCExpr *SubExprVal;
1797 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001798 return true;
1799
Evan Cheng75972122011-01-13 07:58:56 +00001800 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1801 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001802 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001803 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001804 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001805 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001806 }
1807}
1808
Evan Cheng75972122011-01-13 07:58:56 +00001809// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1810// :lower16: and :upper16:.
1811bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1812 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001813
1814 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001815 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001816 Parser.Lex(); // Eat ':'
1817
1818 if (getLexer().isNot(AsmToken::Identifier)) {
1819 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1820 return true;
1821 }
1822
1823 StringRef IDVal = Parser.getTok().getIdentifier();
1824 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001825 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001826 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001827 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001828 } else {
1829 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1830 return true;
1831 }
1832 Parser.Lex();
1833
1834 if (getLexer().isNot(AsmToken::Colon)) {
1835 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1836 return true;
1837 }
1838 Parser.Lex(); // Eat the last ':'
1839 return false;
1840}
1841
1842const MCExpr *
1843ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1844 MCSymbolRefExpr::VariantKind Variant) {
1845 // Recurse over the given expression, rebuilding it to apply the given variant
1846 // to the leftmost symbol.
1847 if (Variant == MCSymbolRefExpr::VK_None)
1848 return E;
1849
1850 switch (E->getKind()) {
1851 case MCExpr::Target:
1852 llvm_unreachable("Can't handle target expr yet");
1853 case MCExpr::Constant:
1854 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1855
1856 case MCExpr::SymbolRef: {
1857 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1858
1859 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1860 return 0;
1861
1862 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1863 }
1864
1865 case MCExpr::Unary:
1866 llvm_unreachable("Can't handle unary expressions yet");
1867
1868 case MCExpr::Binary: {
1869 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1870 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1871 const MCExpr *RHS = BE->getRHS();
1872 if (!LHS)
1873 return 0;
1874
1875 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1876 }
1877 }
1878
1879 assert(0 && "Invalid expression kind!");
1880 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001881}
1882
Daniel Dunbar352e1482011-01-11 15:59:50 +00001883/// \brief Given a mnemonic, split out possible predication code and carry
1884/// setting letters to form a canonical mnemonic and flags.
1885//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001886// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001887static StringRef SplitMnemonic(StringRef Mnemonic,
1888 unsigned &PredicationCode,
1889 bool &CarrySetting,
1890 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001891 PredicationCode = ARMCC::AL;
1892 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001893 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001894
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001895 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001896 //
1897 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001898 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1899 Mnemonic == "movs" ||
1900 Mnemonic == "svc" ||
1901 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1902 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1903 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1904 Mnemonic == "vclt" ||
1905 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1906 Mnemonic == "vcle" ||
1907 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1908 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbachd1f0bbe2011-06-27 20:59:10 +00001909 Mnemonic == "vqdmlal" || Mnemonic == "bics"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001910 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001911
Jim Grosbach3f00e312011-07-11 17:09:57 +00001912 // First, split out any predication code. Ignore mnemonics we know aren't
1913 // predicated but do have a carry-set and so weren't caught above.
1914 if (Mnemonic != "adcs") {
1915 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1916 .Case("eq", ARMCC::EQ)
1917 .Case("ne", ARMCC::NE)
1918 .Case("hs", ARMCC::HS)
1919 .Case("cs", ARMCC::HS)
1920 .Case("lo", ARMCC::LO)
1921 .Case("cc", ARMCC::LO)
1922 .Case("mi", ARMCC::MI)
1923 .Case("pl", ARMCC::PL)
1924 .Case("vs", ARMCC::VS)
1925 .Case("vc", ARMCC::VC)
1926 .Case("hi", ARMCC::HI)
1927 .Case("ls", ARMCC::LS)
1928 .Case("ge", ARMCC::GE)
1929 .Case("lt", ARMCC::LT)
1930 .Case("gt", ARMCC::GT)
1931 .Case("le", ARMCC::LE)
1932 .Case("al", ARMCC::AL)
1933 .Default(~0U);
1934 if (CC != ~0U) {
1935 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
1936 PredicationCode = CC;
1937 }
Bill Wendling52925b62010-10-29 23:50:21 +00001938 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001939
Daniel Dunbar352e1482011-01-11 15:59:50 +00001940 // Next, determine if we have a carry setting bit. We explicitly ignore all
1941 // the instructions we know end in 's'.
1942 if (Mnemonic.endswith("s") &&
1943 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1944 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1945 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1946 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1947 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1948 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1949 CarrySetting = true;
1950 }
1951
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001952 // The "cps" instruction can have a interrupt mode operand which is glued into
1953 // the mnemonic. Check if this is the case, split it and parse the imod op
1954 if (Mnemonic.startswith("cps")) {
1955 // Split out any imod code.
1956 unsigned IMod =
1957 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1958 .Case("ie", ARM_PROC::IE)
1959 .Case("id", ARM_PROC::ID)
1960 .Default(~0U);
1961 if (IMod != ~0U) {
1962 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1963 ProcessorIMod = IMod;
1964 }
1965 }
1966
Daniel Dunbar352e1482011-01-11 15:59:50 +00001967 return Mnemonic;
1968}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001969
1970/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1971/// inclusion of carry set or predication code operands.
1972//
1973// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001974void ARMAsmParser::
1975GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1976 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001977 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1978 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1979 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1980 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001981 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001982 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1983 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001984 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00001985 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001986 CanAcceptCarrySet = true;
1987 } else {
1988 CanAcceptCarrySet = false;
1989 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001990
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001991 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1992 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1993 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1994 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001995 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001996 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001997 CanAcceptPredicationCode = false;
1998 } else {
1999 CanAcceptPredicationCode = true;
2000 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002001
Evan Chengebdeeab2011-07-08 01:53:10 +00002002 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002003 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002004 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002005 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002006}
2007
2008/// Parse an arm instruction mnemonic followed by its operands.
2009bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2010 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2011 // Create the leading tokens for the mnemonic, split by '.' characters.
2012 size_t Start = 0, Next = Name.find('.');
2013 StringRef Head = Name.slice(Start, Next);
2014
Daniel Dunbar352e1482011-01-11 15:59:50 +00002015 // Split out the predication code and carry setting flag from the mnemonic.
2016 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002017 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002018 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002019 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
2020 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002021
Chris Lattner3a697562010-10-28 17:20:03 +00002022 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00002023
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002024 // Next, add the CCOut and ConditionCode operands, if needed.
2025 //
2026 // For mnemonics which can ever incorporate a carry setting bit or predication
2027 // code, our matching model involves us always generating CCOut and
2028 // ConditionCode operands to match the mnemonic "as written" and then we let
2029 // the matcher deal with finding the right instruction or generating an
2030 // appropriate error.
2031 bool CanAcceptCarrySet, CanAcceptPredicationCode;
2032 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
2033
2034 // Add the carry setting operand, if necessary.
2035 //
2036 // FIXME: It would be awesome if we could somehow invent a location such that
2037 // match errors on this operand would print a nice diagnostic about how the
2038 // 's' character in the mnemonic resulted in a CCOut operand.
2039 if (CanAcceptCarrySet) {
2040 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2041 NameLoc));
2042 } else {
2043 // This mnemonic can't ever accept a carry set, but the user wrote one (or
2044 // misspelled another mnemonic).
2045
2046 // FIXME: Issue a nice error.
2047 }
2048
2049 // Add the predication code operand, if necessary.
2050 if (CanAcceptPredicationCode) {
2051 Operands.push_back(ARMOperand::CreateCondCode(
2052 ARMCC::CondCodes(PredicationCode), NameLoc));
2053 } else {
2054 // This mnemonic can't ever accept a predication code, but the user wrote
2055 // one (or misspelled another mnemonic).
2056
2057 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002058 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002059
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002060 // Add the processor imod operand, if necessary.
2061 if (ProcessorIMod) {
2062 Operands.push_back(ARMOperand::CreateImm(
2063 MCConstantExpr::Create(ProcessorIMod, getContext()),
2064 NameLoc, NameLoc));
2065 } else {
2066 // This mnemonic can't ever accept a imod, but the user wrote
2067 // one (or misspelled another mnemonic).
2068
2069 // FIXME: Issue a nice error.
2070 }
2071
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002072 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002073 while (Next != StringRef::npos) {
2074 Start = Next;
2075 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002076 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002077
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002078 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002079 }
2080
2081 // Read the remaining operands.
2082 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002083 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002084 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002085 Parser.EatToEndOfStatement();
2086 return true;
2087 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002088
2089 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002090 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002091
2092 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002093 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002094 Parser.EatToEndOfStatement();
2095 return true;
2096 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002097 }
2098 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002099
Chris Lattnercbf8a982010-09-11 16:18:25 +00002100 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2101 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002102 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002103 }
Bill Wendling146018f2010-11-06 21:42:12 +00002104
Chris Lattner34e53142010-09-08 05:10:46 +00002105 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00002106 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002107}
2108
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002109bool ARMAsmParser::
2110MatchAndEmitInstruction(SMLoc IDLoc,
2111 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2112 MCStreamer &Out) {
2113 MCInst Inst;
2114 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002115 MatchResultTy MatchResult, MatchResult2;
2116 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2117 if (MatchResult != Match_Success) {
2118 // If we get a Match_InvalidOperand it might be some arithmetic instruction
2119 // that does not update the condition codes. So try adding a CCOut operand
2120 // with a value of reg0.
2121 if (MatchResult == Match_InvalidOperand) {
2122 Operands.insert(Operands.begin() + 1,
2123 ARMOperand::CreateCCOut(0,
2124 ((ARMOperand*)Operands[0])->getStartLoc()));
2125 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2126 if (MatchResult2 == Match_Success)
2127 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002128 else {
2129 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002130 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002131 delete CCOut;
2132 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002133 }
2134 // If we get a Match_MnemonicFail it might be some arithmetic instruction
2135 // that updates the condition codes if it ends in 's'. So see if the
2136 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
2137 // operand with a value of CPSR.
Evan Chengeb0caa12011-07-08 22:49:55 +00002138 else if (MatchResult == Match_MnemonicFail) {
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002139 // Get the instruction mnemonic, which is the first token.
2140 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
2141 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
2142 // removed the 's' from the mnemonic for matching.
2143 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
2144 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002145 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2146 Operands.erase(Operands.begin());
2147 delete OldMnemonic;
2148 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002149 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
2150 Operands.insert(Operands.begin() + 1,
2151 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2152 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2153 if (MatchResult2 == Match_Success)
2154 MatchResult = Match_Success;
2155 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002156 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2157 Operands.erase(Operands.begin());
2158 delete OldMnemonic;
2159 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002160 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002161 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2162 Operands.erase(Operands.begin() + 1);
2163 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002164 }
2165 }
2166 }
2167 }
2168 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002169 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002170 Out.EmitInstruction(Inst);
2171 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002172 case Match_MissingFeature:
2173 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2174 return true;
2175 case Match_InvalidOperand: {
2176 SMLoc ErrorLoc = IDLoc;
2177 if (ErrorInfo != ~0U) {
2178 if (ErrorInfo >= Operands.size())
2179 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002180
Chris Lattnere73d4f82010-10-28 21:41:58 +00002181 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2182 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2183 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002184
Chris Lattnere73d4f82010-10-28 21:41:58 +00002185 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002186 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002187 case Match_MnemonicFail:
2188 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002189 case Match_ConversionFail:
2190 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002191 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002192
Eric Christopherc223e2b2010-10-29 09:26:59 +00002193 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002194 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002195}
2196
Kevin Enderby515d5092009-10-15 20:48:48 +00002197/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002198bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2199 StringRef IDVal = DirectiveID.getIdentifier();
2200 if (IDVal == ".word")
2201 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002202 else if (IDVal == ".thumb")
2203 return ParseDirectiveThumb(DirectiveID.getLoc());
2204 else if (IDVal == ".thumb_func")
2205 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2206 else if (IDVal == ".code")
2207 return ParseDirectiveCode(DirectiveID.getLoc());
2208 else if (IDVal == ".syntax")
2209 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002210 return true;
2211}
2212
2213/// ParseDirectiveWord
2214/// ::= .word [ expression (, expression)* ]
2215bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2216 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2217 for (;;) {
2218 const MCExpr *Value;
2219 if (getParser().ParseExpression(Value))
2220 return true;
2221
Chris Lattneraaec2052010-01-19 19:46:13 +00002222 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002223
2224 if (getLexer().is(AsmToken::EndOfStatement))
2225 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002226
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002227 // FIXME: Improve diagnostic.
2228 if (getLexer().isNot(AsmToken::Comma))
2229 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002230 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002231 }
2232 }
2233
Sean Callananb9a25b72010-01-19 20:27:46 +00002234 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002235 return false;
2236}
2237
Kevin Enderby515d5092009-10-15 20:48:48 +00002238/// ParseDirectiveThumb
2239/// ::= .thumb
2240bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2241 if (getLexer().isNot(AsmToken::EndOfStatement))
2242 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002243 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002244
2245 // TODO: set thumb mode
2246 // TODO: tell the MC streamer the mode
2247 // getParser().getStreamer().Emit???();
2248 return false;
2249}
2250
2251/// ParseDirectiveThumbFunc
2252/// ::= .thumbfunc symbol_name
2253bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002254 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2255 bool isMachO = MAI.hasSubsectionsViaSymbols();
2256 StringRef Name;
2257
2258 // Darwin asm has function name after .thumb_func direction
2259 // ELF doesn't
2260 if (isMachO) {
2261 const AsmToken &Tok = Parser.getTok();
2262 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2263 return Error(L, "unexpected token in .thumb_func directive");
2264 Name = Tok.getString();
2265 Parser.Lex(); // Consume the identifier token.
2266 }
2267
Kevin Enderby515d5092009-10-15 20:48:48 +00002268 if (getLexer().isNot(AsmToken::EndOfStatement))
2269 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002270 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002271
Rafael Espindola64695402011-05-16 16:17:21 +00002272 // FIXME: assuming function name will be the line following .thumb_func
2273 if (!isMachO) {
2274 Name = Parser.getTok().getString();
2275 }
2276
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002277 // Mark symbol as a thumb symbol.
2278 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2279 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002280 return false;
2281}
2282
2283/// ParseDirectiveSyntax
2284/// ::= .syntax unified | divided
2285bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002286 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002287 if (Tok.isNot(AsmToken::Identifier))
2288 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002289 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002290 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002291 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002292 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002293 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002294 else
2295 return Error(L, "unrecognized syntax mode in .syntax directive");
2296
2297 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002298 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002299 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002300
2301 // TODO tell the MC streamer the mode
2302 // getParser().getStreamer().Emit???();
2303 return false;
2304}
2305
2306/// ParseDirectiveCode
2307/// ::= .code 16 | 32
2308bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002309 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002310 if (Tok.isNot(AsmToken::Integer))
2311 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002312 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002313 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002314 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002315 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002316 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002317 else
2318 return Error(L, "invalid operand to .code directive");
2319
2320 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002321 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002322 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002323
Evan Cheng32869202011-07-08 22:36:29 +00002324 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002325 if (!isThumb())
2326 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002327 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002328 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002329 if (isThumb())
2330 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002331 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002332 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002333
Kevin Enderby515d5092009-10-15 20:48:48 +00002334 return false;
2335}
2336
Sean Callanan90b70972010-04-07 20:29:34 +00002337extern "C" void LLVMInitializeARMAsmLexer();
2338
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002339/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002340extern "C" void LLVMInitializeARMAsmParser() {
2341 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2342 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002343 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002344}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002345
Chris Lattner0692ee62010-09-06 19:11:01 +00002346#define GET_REGISTER_MATCHER
2347#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002348#include "ARMGenAsmMatcher.inc"