blob: 0ce4b4c128284cf83c69d628c438cd648d661be4 [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 }
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000389 bool isImm0_65535() 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 Value >= 0 && Value < 65536;
396 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000397 bool isT2SOImm() const {
398 if (Kind != Immediate)
399 return false;
400 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
401 if (!CE) return false;
402 int64_t Value = CE->getValue();
403 return ARM_AM::getT2SOImmVal(Value) != -1;
404 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000405 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000406 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000407 bool isDPRRegList() const { return Kind == DPRRegisterList; }
408 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000409 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000410 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000411 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000412 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000413 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000414 bool isMemMode2() const {
415 if (getMemAddrMode() != ARMII::AddrMode2)
416 return false;
417
418 if (getMemOffsetIsReg())
419 return true;
420
421 if (getMemNegative() &&
422 !(getMemPostindexed() || getMemPreindexed()))
423 return false;
424
425 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
426 if (!CE) return false;
427 int64_t Value = CE->getValue();
428
429 // The offset must be in the range 0-4095 (imm12).
430 if (Value > 4095 || Value < -4095)
431 return false;
432
433 return true;
434 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000435 bool isMemMode3() const {
436 if (getMemAddrMode() != ARMII::AddrMode3)
437 return false;
438
439 if (getMemOffsetIsReg()) {
440 if (getMemOffsetRegShifted())
441 return false; // No shift with offset reg allowed
442 return true;
443 }
444
445 if (getMemNegative() &&
446 !(getMemPostindexed() || getMemPreindexed()))
447 return false;
448
449 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
450 if (!CE) return false;
451 int64_t Value = CE->getValue();
452
453 // The offset must be in the range 0-255 (imm8).
454 if (Value > 255 || Value < -255)
455 return false;
456
457 return true;
458 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000459 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000460 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
461 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000462 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000463
Daniel Dunbar4b462672011-01-18 05:55:27 +0000464 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000465 if (!CE) return false;
466
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000467 // The offset must be a multiple of 4 in the range 0-1020.
468 int64_t Value = CE->getValue();
469 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
470 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000471 bool isMemMode7() const {
472 if (!isMemory() ||
473 getMemPreindexed() ||
474 getMemPostindexed() ||
475 getMemOffsetIsReg() ||
476 getMemNegative() ||
477 getMemWriteback())
478 return false;
479
480 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
481 if (!CE) return false;
482
483 if (CE->getValue())
484 return false;
485
486 return true;
487 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000488 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000489 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000490 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000491 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000492 }
493 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000494 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000495 return false;
496
Daniel Dunbar4b462672011-01-18 05:55:27 +0000497 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000498 if (!CE) return false;
499
500 // The offset must be a multiple of 4 in the range 0-124.
501 uint64_t Value = CE->getValue();
502 return ((Value & 0x3) == 0 && Value <= 124);
503 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000504 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000505 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000506
507 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000508 // Add as immediates when possible. Null MCExpr = 0.
509 if (Expr == 0)
510 Inst.addOperand(MCOperand::CreateImm(0));
511 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000512 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
513 else
514 Inst.addOperand(MCOperand::CreateExpr(Expr));
515 }
516
Daniel Dunbar8462b302010-08-11 06:36:53 +0000517 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000518 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000519 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000520 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
521 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000522 }
523
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000524 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
525 assert(N == 1 && "Invalid number of operands!");
526 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
527 }
528
529 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
530 assert(N == 1 && "Invalid number of operands!");
531 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
532 }
533
Jim Grosbachd67641b2010-12-06 18:21:12 +0000534 void addCCOutOperands(MCInst &Inst, unsigned N) const {
535 assert(N == 1 && "Invalid number of operands!");
536 Inst.addOperand(MCOperand::CreateReg(getReg()));
537 }
538
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000539 void addRegOperands(MCInst &Inst, unsigned N) const {
540 assert(N == 1 && "Invalid number of operands!");
541 Inst.addOperand(MCOperand::CreateReg(getReg()));
542 }
543
Jim Grosbache8606dc2011-07-13 17:50:29 +0000544 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
545 assert(N == 3 && "Invalid number of operands!");
546 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
547 assert((ShiftedReg.ShiftReg == 0 ||
548 ARM_AM::getSORegOffset(ShiftedReg.ShiftImm) == 0) &&
549 "Invalid shifted register operand!");
550 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
551 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
552 Inst.addOperand(MCOperand::CreateImm(
553 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
554 }
555
Owen Anderson00828302011-03-18 22:50:18 +0000556 void addShifterOperands(MCInst &Inst, unsigned N) const {
557 assert(N == 1 && "Invalid number of operands!");
558 Inst.addOperand(MCOperand::CreateImm(
559 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
560 }
561
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000562 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000563 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000564 const SmallVectorImpl<unsigned> &RegList = getRegList();
565 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000566 I = RegList.begin(), E = RegList.end(); I != E; ++I)
567 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000568 }
569
Bill Wendling0f630752010-11-17 04:32:08 +0000570 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
571 addRegListOperands(Inst, N);
572 }
573
574 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
575 addRegListOperands(Inst, N);
576 }
577
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000578 void addImmOperands(MCInst &Inst, unsigned N) const {
579 assert(N == 1 && "Invalid number of operands!");
580 addExpr(Inst, getImm());
581 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000582
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000583 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
584 assert(N == 1 && "Invalid number of operands!");
585 addExpr(Inst, getImm());
586 }
587
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000588 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
589 assert(N == 1 && "Invalid number of operands!");
590 addExpr(Inst, getImm());
591 }
592
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000593 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
594 assert(N == 1 && "Invalid number of operands!");
595 addExpr(Inst, getImm());
596 }
597
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000598 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
599 assert(N == 1 && "Invalid number of operands!");
600 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
601 }
602
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000603 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
604 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
605 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
606
607 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000608 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000609 assert((CE || CE->getValue() == 0) &&
610 "No offset operand support in mode 7");
611 }
612
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000613 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
614 assert(isMemMode2() && "Invalid mode or number of operands!");
615 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
616 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
617
618 if (getMemOffsetIsReg()) {
619 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
620
621 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
622 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
623 int64_t ShiftAmount = 0;
624
625 if (getMemOffsetRegShifted()) {
626 ShOpc = getMemShiftType();
627 const MCConstantExpr *CE =
628 dyn_cast<MCConstantExpr>(getMemShiftAmount());
629 ShiftAmount = CE->getValue();
630 }
631
632 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
633 ShOpc, IdxMode)));
634 return;
635 }
636
637 // Create a operand placeholder to always yield the same number of operands.
638 Inst.addOperand(MCOperand::CreateReg(0));
639
640 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
641 // the difference?
642 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
643 assert(CE && "Non-constant mode 2 offset operand!");
644 int64_t Offset = CE->getValue();
645
646 if (Offset >= 0)
647 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
648 Offset, ARM_AM::no_shift, IdxMode)));
649 else
650 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
651 -Offset, ARM_AM::no_shift, IdxMode)));
652 }
653
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000654 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
655 assert(isMemMode3() && "Invalid mode or number of operands!");
656 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
657 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
658
659 if (getMemOffsetIsReg()) {
660 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
661
662 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
663 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
664 IdxMode)));
665 return;
666 }
667
668 // Create a operand placeholder to always yield the same number of operands.
669 Inst.addOperand(MCOperand::CreateReg(0));
670
671 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
672 // the difference?
673 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
674 assert(CE && "Non-constant mode 3 offset operand!");
675 int64_t Offset = CE->getValue();
676
677 if (Offset >= 0)
678 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
679 Offset, IdxMode)));
680 else
681 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
682 -Offset, IdxMode)));
683 }
684
Chris Lattner14b93852010-10-29 00:27:31 +0000685 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
686 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000687
Daniel Dunbar4b462672011-01-18 05:55:27 +0000688 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
689 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000690
Jim Grosbach80eb2332010-10-29 17:41:25 +0000691 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
692 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000693 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000694 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000695
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000696 // The MCInst offset operand doesn't include the low two bits (like
697 // the instruction encoding).
698 int64_t Offset = CE->getValue() / 4;
699 if (Offset >= 0)
700 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
701 Offset)));
702 else
703 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
704 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000705 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000706
Bill Wendlingf4caf692010-12-14 03:36:38 +0000707 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
708 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000709 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
710 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000711 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000712
Bill Wendlingf4caf692010-12-14 03:36:38 +0000713 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
714 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000715 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
716 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000717 assert(CE && "Non-constant mode offset operand!");
718 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000719 }
720
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000721 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
722 assert(N == 1 && "Invalid number of operands!");
723 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
724 }
725
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000726 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
727 assert(N == 1 && "Invalid number of operands!");
728 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
729 }
730
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000731 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000732
Chris Lattner3a697562010-10-28 17:20:03 +0000733 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
734 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000735 Op->CC.Val = CC;
736 Op->StartLoc = S;
737 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000738 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000739 }
740
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000741 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
742 ARMOperand *Op = new ARMOperand(CoprocNum);
743 Op->Cop.Val = CopVal;
744 Op->StartLoc = S;
745 Op->EndLoc = S;
746 return Op;
747 }
748
749 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
750 ARMOperand *Op = new ARMOperand(CoprocReg);
751 Op->Cop.Val = CopVal;
752 Op->StartLoc = S;
753 Op->EndLoc = S;
754 return Op;
755 }
756
Jim Grosbachd67641b2010-12-06 18:21:12 +0000757 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
758 ARMOperand *Op = new ARMOperand(CCOut);
759 Op->Reg.RegNum = RegNum;
760 Op->StartLoc = S;
761 Op->EndLoc = S;
762 return Op;
763 }
764
Chris Lattner3a697562010-10-28 17:20:03 +0000765 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
766 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000767 Op->Tok.Data = Str.data();
768 Op->Tok.Length = Str.size();
769 Op->StartLoc = S;
770 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000771 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000772 }
773
Bill Wendling50d0f582010-11-18 23:43:05 +0000774 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000775 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000776 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000777 Op->StartLoc = S;
778 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000779 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000780 }
781
Jim Grosbache8606dc2011-07-13 17:50:29 +0000782 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
783 unsigned SrcReg,
784 unsigned ShiftReg,
785 unsigned ShiftImm,
786 SMLoc S, SMLoc E) {
787 ARMOperand *Op = new ARMOperand(ShiftedRegister);
788 Op->ShiftedReg.ShiftTy = ShTy;
789 Op->ShiftedReg.SrcReg = SrcReg;
790 Op->ShiftedReg.ShiftReg = ShiftReg;
791 Op->ShiftedReg.ShiftImm = ShiftImm;
792 Op->StartLoc = S;
793 Op->EndLoc = E;
794 return Op;
795 }
796
Owen Anderson00828302011-03-18 22:50:18 +0000797 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
798 SMLoc S, SMLoc E) {
799 ARMOperand *Op = new ARMOperand(Shifter);
800 Op->Shift.ShiftTy = ShTy;
801 Op->StartLoc = S;
802 Op->EndLoc = E;
803 return Op;
804 }
805
Bill Wendling7729e062010-11-09 22:44:22 +0000806 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000807 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000808 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000809 KindTy Kind = RegisterList;
810
811 if (ARM::DPRRegClass.contains(Regs.front().first))
812 Kind = DPRRegisterList;
813 else if (ARM::SPRRegClass.contains(Regs.front().first))
814 Kind = SPRRegisterList;
815
816 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000817 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000818 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000819 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000820 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000821 Op->StartLoc = StartLoc;
822 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000823 return Op;
824 }
825
Chris Lattner3a697562010-10-28 17:20:03 +0000826 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
827 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000828 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000829 Op->StartLoc = S;
830 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000831 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000832 }
833
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000834 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
835 bool OffsetIsReg, const MCExpr *Offset,
836 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000837 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000838 const MCExpr *ShiftAmount, bool Preindexed,
839 bool Postindexed, bool Negative, bool Writeback,
840 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000841 assert((OffsetRegNum == -1 || OffsetIsReg) &&
842 "OffsetRegNum must imply OffsetIsReg!");
843 assert((!OffsetRegShifted || OffsetIsReg) &&
844 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000845 assert((Offset || OffsetIsReg) &&
846 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000847 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
848 "Cannot have shift amount without shifted register offset!");
849 assert((!Offset || !OffsetIsReg) &&
850 "Cannot have expression offset and register offset!");
851
Chris Lattner3a697562010-10-28 17:20:03 +0000852 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000853 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000854 Op->Mem.BaseRegNum = BaseRegNum;
855 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000856 if (OffsetIsReg)
857 Op->Mem.Offset.RegNum = OffsetRegNum;
858 else
859 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000860 Op->Mem.OffsetRegShifted = OffsetRegShifted;
861 Op->Mem.ShiftType = ShiftType;
862 Op->Mem.ShiftAmount = ShiftAmount;
863 Op->Mem.Preindexed = Preindexed;
864 Op->Mem.Postindexed = Postindexed;
865 Op->Mem.Negative = Negative;
866 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000867
Sean Callanan76264762010-04-02 22:27:05 +0000868 Op->StartLoc = S;
869 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000870 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000871 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000872
873 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
874 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
875 Op->MBOpt.Val = Opt;
876 Op->StartLoc = S;
877 Op->EndLoc = S;
878 return Op;
879 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000880
881 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
882 ARMOperand *Op = new ARMOperand(ProcIFlags);
883 Op->IFlags.Val = IFlags;
884 Op->StartLoc = S;
885 Op->EndLoc = S;
886 return Op;
887 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000888
889 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
890 ARMOperand *Op = new ARMOperand(MSRMask);
891 Op->MMask.Val = MMask;
892 Op->StartLoc = S;
893 Op->EndLoc = S;
894 return Op;
895 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000896};
897
898} // end anonymous namespace.
899
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000900void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000901 switch (Kind) {
902 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000903 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000904 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000905 case CCOut:
906 OS << "<ccout " << getReg() << ">";
907 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000908 case CoprocNum:
909 OS << "<coprocessor number: " << getCoproc() << ">";
910 break;
911 case CoprocReg:
912 OS << "<coprocessor register: " << getCoproc() << ">";
913 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000914 case MSRMask:
915 OS << "<mask: " << getMSRMask() << ">";
916 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000917 case Immediate:
918 getImm()->print(OS);
919 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000920 case MemBarrierOpt:
921 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
922 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000923 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000924 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000925 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
926 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000927 if (getMemOffsetIsReg()) {
928 OS << " offset:<register " << getMemOffsetRegNum();
929 if (getMemOffsetRegShifted()) {
930 OS << " offset-shift-type:" << getMemShiftType();
931 OS << " offset-shift-amount:" << *getMemShiftAmount();
932 }
933 } else {
934 OS << " offset:" << *getMemOffset();
935 }
936 if (getMemOffsetIsReg())
937 OS << " (offset-is-reg)";
938 if (getMemPreindexed())
939 OS << " (pre-indexed)";
940 if (getMemPostindexed())
941 OS << " (post-indexed)";
942 if (getMemNegative())
943 OS << " (negative)";
944 if (getMemWriteback())
945 OS << " (writeback)";
946 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000947 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000948 case ProcIFlags: {
949 OS << "<ARM_PROC::";
950 unsigned IFlags = getProcIFlags();
951 for (int i=2; i >= 0; --i)
952 if (IFlags & (1 << i))
953 OS << ARM_PROC::IFlagsToString(1 << i);
954 OS << ">";
955 break;
956 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000957 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000958 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000959 break;
Owen Anderson00828302011-03-18 22:50:18 +0000960 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +0000961 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
962 break;
963 case ShiftedRegister:
964 OS << "<so_reg"
965 << ShiftedReg.SrcReg
966 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
967 << ", " << ShiftedReg.ShiftReg << ", "
968 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
969 << ">";
Owen Anderson00828302011-03-18 22:50:18 +0000970 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000971 case RegisterList:
972 case DPRRegisterList:
973 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000974 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000975
Bill Wendling5fa22a12010-11-09 23:28:44 +0000976 const SmallVectorImpl<unsigned> &RegList = getRegList();
977 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000978 I = RegList.begin(), E = RegList.end(); I != E; ) {
979 OS << *I;
980 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000981 }
982
983 OS << ">";
984 break;
985 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000986 case Token:
987 OS << "'" << getToken() << "'";
988 break;
989 }
990}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000991
992/// @name Auto-generated Match Functions
993/// {
994
995static unsigned MatchRegisterName(StringRef Name);
996
997/// }
998
Bob Wilson69df7232011-02-03 21:46:10 +0000999bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1000 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +00001001 RegNo = TryParseRegister();
1002
1003 return (RegNo == (unsigned)-1);
1004}
1005
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001006/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00001007/// and if it is a register name the token is eaten and the register number is
1008/// returned. Otherwise return -1.
1009///
1010int ARMAsmParser::TryParseRegister() {
1011 const AsmToken &Tok = Parser.getTok();
1012 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001013
Chris Lattnere5658fa2010-10-30 04:09:10 +00001014 // FIXME: Validate register for the current architecture; we have to do
1015 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001016 std::string upperCase = Tok.getString().str();
1017 std::string lowerCase = LowercaseString(upperCase);
1018 unsigned RegNum = MatchRegisterName(lowerCase);
1019 if (!RegNum) {
1020 RegNum = StringSwitch<unsigned>(lowerCase)
1021 .Case("r13", ARM::SP)
1022 .Case("r14", ARM::LR)
1023 .Case("r15", ARM::PC)
1024 .Case("ip", ARM::R12)
1025 .Default(0);
1026 }
1027 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001028
Chris Lattnere5658fa2010-10-30 04:09:10 +00001029 Parser.Lex(); // Eat identifier token.
1030 return RegNum;
1031}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001032
Jim Grosbach19906722011-07-13 18:49:30 +00001033// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1034// If a recoverable error occurs, return 1. If an irrecoverable error
1035// occurs, return -1. An irrecoverable error is one where tokens have been
1036// consumed in the process of trying to parse the shifter (i.e., when it is
1037// indeed a shifter operand, but malformed).
1038int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001039 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1040 SMLoc S = Parser.getTok().getLoc();
1041 const AsmToken &Tok = Parser.getTok();
1042 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1043
1044 std::string upperCase = Tok.getString().str();
1045 std::string lowerCase = LowercaseString(upperCase);
1046 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1047 .Case("lsl", ARM_AM::lsl)
1048 .Case("lsr", ARM_AM::lsr)
1049 .Case("asr", ARM_AM::asr)
1050 .Case("ror", ARM_AM::ror)
1051 .Case("rrx", ARM_AM::rrx)
1052 .Default(ARM_AM::no_shift);
1053
1054 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001055 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001056
Jim Grosbache8606dc2011-07-13 17:50:29 +00001057 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001058
Jim Grosbache8606dc2011-07-13 17:50:29 +00001059 // The source register for the shift has already been added to the
1060 // operand list, so we need to pop it off and combine it into the shifted
1061 // register operand instead.
1062 ARMOperand *PrevOp = (ARMOperand*)Operands.pop_back_val();
1063 if (!PrevOp->isReg())
1064 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1065 int SrcReg = PrevOp->getReg();
1066 int64_t Imm = 0;
1067 int ShiftReg = 0;
1068 if (ShiftTy == ARM_AM::rrx) {
1069 // RRX Doesn't have an explicit shift amount. The encoder expects
1070 // the shift register to be the same as the source register. Seems odd,
1071 // but OK.
1072 ShiftReg = SrcReg;
1073 } else {
1074 // Figure out if this is shifted by a constant or a register (for non-RRX).
1075 if (Parser.getTok().is(AsmToken::Hash)) {
1076 Parser.Lex(); // Eat hash.
1077 SMLoc ImmLoc = Parser.getTok().getLoc();
1078 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001079 if (getParser().ParseExpression(ShiftExpr)) {
1080 Error(ImmLoc, "invalid immediate shift value");
1081 return -1;
1082 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001083 // The expression must be evaluatable as an immediate.
1084 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001085 if (!CE) {
1086 Error(ImmLoc, "invalid immediate shift value");
1087 return -1;
1088 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001089 // Range check the immediate.
1090 // lsl, ror: 0 <= imm <= 31
1091 // lsr, asr: 0 <= imm <= 32
1092 Imm = CE->getValue();
1093 if (Imm < 0 ||
1094 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1095 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001096 Error(ImmLoc, "immediate shift value out of range");
1097 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001098 }
1099 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1100 ShiftReg = TryParseRegister();
1101 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001102 if (ShiftReg == -1) {
1103 Error (L, "expected immediate or register in shift operand");
1104 return -1;
1105 }
1106 } else {
1107 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001108 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001109 return -1;
1110 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001111 }
1112
Jim Grosbache8606dc2011-07-13 17:50:29 +00001113 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1114 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001115 S, Parser.getTok().getLoc()));
1116
Jim Grosbach19906722011-07-13 18:49:30 +00001117 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001118}
1119
1120
Bill Wendling50d0f582010-11-18 23:43:05 +00001121/// Try to parse a register name. The token must be an Identifier when called.
1122/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1123/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001124///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001125/// TODO this is likely to change to allow different register types and or to
1126/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001127bool ARMAsmParser::
1128TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001129 SMLoc S = Parser.getTok().getLoc();
1130 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001131 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001132 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001133
Bill Wendling50d0f582010-11-18 23:43:05 +00001134 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001135
Chris Lattnere5658fa2010-10-30 04:09:10 +00001136 const AsmToken &ExclaimTok = Parser.getTok();
1137 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001138 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1139 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001140 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001141 }
1142
Bill Wendling50d0f582010-11-18 23:43:05 +00001143 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001144}
1145
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001146/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1147/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1148/// "c5", ...
1149static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001150 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1151 // but efficient.
1152 switch (Name.size()) {
1153 default: break;
1154 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001155 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001156 return -1;
1157 switch (Name[1]) {
1158 default: return -1;
1159 case '0': return 0;
1160 case '1': return 1;
1161 case '2': return 2;
1162 case '3': return 3;
1163 case '4': return 4;
1164 case '5': return 5;
1165 case '6': return 6;
1166 case '7': return 7;
1167 case '8': return 8;
1168 case '9': return 9;
1169 }
1170 break;
1171 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001172 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001173 return -1;
1174 switch (Name[2]) {
1175 default: return -1;
1176 case '0': return 10;
1177 case '1': return 11;
1178 case '2': return 12;
1179 case '3': return 13;
1180 case '4': return 14;
1181 case '5': return 15;
1182 }
1183 break;
1184 }
1185
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001186 return -1;
1187}
1188
Jim Grosbachf922c472011-02-12 01:34:40 +00001189/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001190/// token must be an Identifier when called, and if it is a coprocessor
1191/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001192ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1193tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001194 SMLoc S = Parser.getTok().getLoc();
1195 const AsmToken &Tok = Parser.getTok();
1196 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1197
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001198 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001199 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001200 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001201
1202 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001203 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001204 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001205}
1206
Jim Grosbachf922c472011-02-12 01:34:40 +00001207/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001208/// token must be an Identifier when called, and if it is a coprocessor
1209/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001210ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1211tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001212 SMLoc S = Parser.getTok().getLoc();
1213 const AsmToken &Tok = Parser.getTok();
1214 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1215
1216 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1217 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001218 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001219
1220 Parser.Lex(); // Eat identifier token.
1221 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001222 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001223}
1224
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001225/// Parse a register list, return it if successful else return null. The first
1226/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001227bool ARMAsmParser::
1228ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001229 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001230 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001231 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001232
Bill Wendling7729e062010-11-09 22:44:22 +00001233 // Read the rest of the registers in the list.
1234 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001235 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001236
Bill Wendling7729e062010-11-09 22:44:22 +00001237 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001238 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001239 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001240
Sean Callanan18b83232010-01-19 21:44:56 +00001241 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001242 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001243 if (RegTok.isNot(AsmToken::Identifier)) {
1244 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001245 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001246 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001247
Bill Wendling1d6a2652010-11-06 10:40:24 +00001248 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001249 if (RegNum == -1) {
1250 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001251 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001252 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001253
Bill Wendlinge7176102010-11-06 22:36:58 +00001254 if (IsRange) {
1255 int Reg = PrevRegNum;
1256 do {
1257 ++Reg;
1258 Registers.push_back(std::make_pair(Reg, RegLoc));
1259 } while (Reg != RegNum);
1260 } else {
1261 Registers.push_back(std::make_pair(RegNum, RegLoc));
1262 }
1263
1264 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001265 } while (Parser.getTok().is(AsmToken::Comma) ||
1266 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001267
1268 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001269 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001270 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1271 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001272 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001273 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001274
Bill Wendlinge7176102010-11-06 22:36:58 +00001275 SMLoc E = RCurlyTok.getLoc();
1276 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001277
Bill Wendlinge7176102010-11-06 22:36:58 +00001278 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001279 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001280 RI = Registers.begin(), RE = Registers.end();
1281
Bill Wendling7caebff2011-01-12 21:20:59 +00001282 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001283 bool EmittedWarning = false;
1284
Bill Wendling7caebff2011-01-12 21:20:59 +00001285 DenseMap<unsigned, bool> RegMap;
1286 RegMap[HighRegNum] = true;
1287
Bill Wendlinge7176102010-11-06 22:36:58 +00001288 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001289 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001290 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001291
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001292 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001293 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001294 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001295 }
1296
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001297 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001298 Warning(RegInfo.second,
1299 "register not in ascending order in register list");
1300
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001301 RegMap[Reg] = true;
1302 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001303 }
1304
Bill Wendling50d0f582010-11-18 23:43:05 +00001305 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1306 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001307}
1308
Jim Grosbachf922c472011-02-12 01:34:40 +00001309/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1310ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1311tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001312 SMLoc S = Parser.getTok().getLoc();
1313 const AsmToken &Tok = Parser.getTok();
1314 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1315 StringRef OptStr = Tok.getString();
1316
1317 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1318 .Case("sy", ARM_MB::SY)
1319 .Case("st", ARM_MB::ST)
1320 .Case("ish", ARM_MB::ISH)
1321 .Case("ishst", ARM_MB::ISHST)
1322 .Case("nsh", ARM_MB::NSH)
1323 .Case("nshst", ARM_MB::NSHST)
1324 .Case("osh", ARM_MB::OSH)
1325 .Case("oshst", ARM_MB::OSHST)
1326 .Default(~0U);
1327
1328 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001329 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001330
1331 Parser.Lex(); // Eat identifier token.
1332 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001333 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001334}
1335
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001336/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001337ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1338tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1339 SMLoc S = Parser.getTok().getLoc();
1340 const AsmToken &Tok = Parser.getTok();
1341 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1342 StringRef IFlagsStr = Tok.getString();
1343
1344 unsigned IFlags = 0;
1345 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1346 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1347 .Case("a", ARM_PROC::A)
1348 .Case("i", ARM_PROC::I)
1349 .Case("f", ARM_PROC::F)
1350 .Default(~0U);
1351
1352 // If some specific iflag is already set, it means that some letter is
1353 // present more than once, this is not acceptable.
1354 if (Flag == ~0U || (IFlags & Flag))
1355 return MatchOperand_NoMatch;
1356
1357 IFlags |= Flag;
1358 }
1359
1360 Parser.Lex(); // Eat identifier token.
1361 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1362 return MatchOperand_Success;
1363}
1364
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001365/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1366ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1367tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1368 SMLoc S = Parser.getTok().getLoc();
1369 const AsmToken &Tok = Parser.getTok();
1370 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1371 StringRef Mask = Tok.getString();
1372
1373 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1374 size_t Start = 0, Next = Mask.find('_');
1375 StringRef Flags = "";
1376 StringRef SpecReg = Mask.slice(Start, Next);
1377 if (Next != StringRef::npos)
1378 Flags = Mask.slice(Next+1, Mask.size());
1379
1380 // FlagsVal contains the complete mask:
1381 // 3-0: Mask
1382 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1383 unsigned FlagsVal = 0;
1384
1385 if (SpecReg == "apsr") {
1386 FlagsVal = StringSwitch<unsigned>(Flags)
1387 .Case("nzcvq", 0x8) // same as CPSR_c
1388 .Case("g", 0x4) // same as CPSR_s
1389 .Case("nzcvqg", 0xc) // same as CPSR_fs
1390 .Default(~0U);
1391
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001392 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001393 if (!Flags.empty())
1394 return MatchOperand_NoMatch;
1395 else
1396 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001397 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001398 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001399 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1400 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001401 for (int i = 0, e = Flags.size(); i != e; ++i) {
1402 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1403 .Case("c", 1)
1404 .Case("x", 2)
1405 .Case("s", 4)
1406 .Case("f", 8)
1407 .Default(~0U);
1408
1409 // If some specific flag is already set, it means that some letter is
1410 // present more than once, this is not acceptable.
1411 if (FlagsVal == ~0U || (FlagsVal & Flag))
1412 return MatchOperand_NoMatch;
1413 FlagsVal |= Flag;
1414 }
1415 } else // No match for special register.
1416 return MatchOperand_NoMatch;
1417
1418 // Special register without flags are equivalent to "fc" flags.
1419 if (!FlagsVal)
1420 FlagsVal = 0x9;
1421
1422 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1423 if (SpecReg == "spsr")
1424 FlagsVal |= 16;
1425
1426 Parser.Lex(); // Eat identifier token.
1427 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1428 return MatchOperand_Success;
1429}
1430
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001431/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1432ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1433tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001434 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001435
1436 if (ParseMemory(Operands, ARMII::AddrMode2))
1437 return MatchOperand_NoMatch;
1438
1439 return MatchOperand_Success;
1440}
1441
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001442/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1443ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1444tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1445 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1446
1447 if (ParseMemory(Operands, ARMII::AddrMode3))
1448 return MatchOperand_NoMatch;
1449
1450 return MatchOperand_Success;
1451}
1452
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001453/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1454/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1455/// when they refer multiple MIOperands inside a single one.
1456bool ARMAsmParser::
1457CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1458 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1459 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1460
1461 // Create a writeback register dummy placeholder.
1462 Inst.addOperand(MCOperand::CreateImm(0));
1463
1464 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1465 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1466 return true;
1467}
1468
1469/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1470/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1471/// when they refer multiple MIOperands inside a single one.
1472bool ARMAsmParser::
1473CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1474 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1475 // Create a writeback register dummy placeholder.
1476 Inst.addOperand(MCOperand::CreateImm(0));
1477 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1478 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1479 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1480 return true;
1481}
1482
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001483/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1484/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1485/// when they refer multiple MIOperands inside a single one.
1486bool ARMAsmParser::
1487CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1488 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1489 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1490
1491 // Create a writeback register dummy placeholder.
1492 Inst.addOperand(MCOperand::CreateImm(0));
1493
1494 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1495 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1496 return true;
1497}
1498
1499/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1500/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1501/// when they refer multiple MIOperands inside a single one.
1502bool ARMAsmParser::
1503CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1504 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1505 // Create a writeback register dummy placeholder.
1506 Inst.addOperand(MCOperand::CreateImm(0));
1507 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1508 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1509 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1510 return true;
1511}
1512
Bill Wendlinge7176102010-11-06 22:36:58 +00001513/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001514/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001515///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001516/// TODO Only preindexing and postindexing addressing are started, unindexed
1517/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001518bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001519ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1520 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001521 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001522 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001523 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001524 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001525 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001526
Sean Callanan18b83232010-01-19 21:44:56 +00001527 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001528 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1529 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001530 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001531 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001532 int BaseRegNum = TryParseRegister();
1533 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001534 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001535 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001536 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001537
Daniel Dunbar05710932011-01-18 05:34:17 +00001538 // The next token must either be a comma or a closing bracket.
1539 const AsmToken &Tok = Parser.getTok();
1540 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1541 return true;
1542
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001543 bool Preindexed = false;
1544 bool Postindexed = false;
1545 bool OffsetIsReg = false;
1546 bool Negative = false;
1547 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001548 ARMOperand *WBOp = 0;
1549 int OffsetRegNum = -1;
1550 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001551 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001552 const MCExpr *ShiftAmount = 0;
1553 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001554
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001555 // First look for preindexed address forms, that is after the "[Rn" we now
1556 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001557 if (Tok.is(AsmToken::Comma)) {
1558 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001559 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001560
Chris Lattner550276e2010-10-28 20:52:15 +00001561 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1562 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001563 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001564 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001565 if (RBracTok.isNot(AsmToken::RBrac)) {
1566 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001567 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001568 }
Sean Callanan76264762010-04-02 22:27:05 +00001569 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001570 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001571
Sean Callanan18b83232010-01-19 21:44:56 +00001572 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001573 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001574 // None of addrmode3 instruction uses "!"
1575 if (AddrMode == ARMII::AddrMode3)
1576 return true;
1577
Bill Wendling50d0f582010-11-18 23:43:05 +00001578 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1579 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001580 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001581 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001582 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1583 if (AddrMode == ARMII::AddrMode2)
1584 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001585 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001586 } else {
1587 // The "[Rn" we have so far was not followed by a comma.
1588
Jim Grosbach80eb2332010-10-29 17:41:25 +00001589 // If there's anything other than the right brace, this is a post indexing
1590 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001591 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001592 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001593
Sean Callanan18b83232010-01-19 21:44:56 +00001594 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001595
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001596 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001597 Postindexed = true;
1598 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001599
Chris Lattner550276e2010-10-28 20:52:15 +00001600 if (NextTok.isNot(AsmToken::Comma)) {
1601 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001602 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001603 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001604
Sean Callananb9a25b72010-01-19 20:27:46 +00001605 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001606
Chris Lattner550276e2010-10-28 20:52:15 +00001607 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001608 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001609 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001610 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001611 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001612 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001613
1614 // Force Offset to exist if used.
1615 if (!OffsetIsReg) {
1616 if (!Offset)
1617 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001618 } else {
1619 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1620 Error(E, "shift amount not supported");
1621 return true;
1622 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001623 }
1624
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001625 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1626 Offset, OffsetRegNum, OffsetRegShifted,
1627 ShiftType, ShiftAmount, Preindexed,
1628 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001629 if (WBOp)
1630 Operands.push_back(WBOp);
1631
1632 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001633}
1634
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001635/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1636/// we will parse the following (were +/- means that a plus or minus is
1637/// optional):
1638/// +/-Rm
1639/// +/-Rm, shift
1640/// #offset
1641/// we return false on success or an error otherwise.
1642bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001643 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001644 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001645 const MCExpr *&ShiftAmount,
1646 const MCExpr *&Offset,
1647 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001648 int &OffsetRegNum,
1649 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001650 Negative = false;
1651 OffsetRegShifted = false;
1652 OffsetIsReg = false;
1653 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001654 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001655 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001656 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001657 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001658 else if (NextTok.is(AsmToken::Minus)) {
1659 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001660 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001661 }
1662 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001663 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001664 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001665 SMLoc CurLoc = OffsetRegTok.getLoc();
1666 OffsetRegNum = TryParseRegister();
1667 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001668 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001669 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001670 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001671 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001672
Bill Wendling12f40e92010-11-06 10:51:53 +00001673 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001674 if (OffsetRegNum != -1) {
1675 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001676 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001677 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001678 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001679
Sean Callanan18b83232010-01-19 21:44:56 +00001680 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001681 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001682 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001683 OffsetRegShifted = true;
1684 }
1685 }
1686 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1687 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001688 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001689 if (HashTok.isNot(AsmToken::Hash))
1690 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001691
Sean Callananb9a25b72010-01-19 20:27:46 +00001692 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001693
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001694 if (getParser().ParseExpression(Offset))
1695 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001696 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001697 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001698 return false;
1699}
1700
1701/// ParseShift as one of these two:
1702/// ( lsl | lsr | asr | ror ) , # shift_amount
1703/// rrx
1704/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001705bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1706 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001707 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001708 if (Tok.isNot(AsmToken::Identifier))
1709 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001710 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001711 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001712 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001713 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001714 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001715 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001716 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001717 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001718 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001719 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001720 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001721 else
1722 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001723 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001724
1725 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001726 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001727 return false;
1728
1729 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001730 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001731 if (HashTok.isNot(AsmToken::Hash))
1732 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001733 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001734
1735 if (getParser().ParseExpression(ShiftAmount))
1736 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001737
1738 return false;
1739}
1740
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001741/// Parse a arm instruction operand. For now this parses the operand regardless
1742/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001743bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001744 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001745 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001746
1747 // Check if the current operand has a custom associated parser, if so, try to
1748 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001749 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1750 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001751 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001752 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1753 // there was a match, but an error occurred, in which case, just return that
1754 // the operand parsing failed.
1755 if (ResTy == MatchOperand_ParseFail)
1756 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001757
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001758 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001759 default:
1760 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001761 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001762 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001763 if (!TryParseRegisterWithWriteBack(Operands))
1764 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001765 int Res = TryParseShiftRegister(Operands);
1766 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001767 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001768 else if (Res == -1) // irrecoverable error
1769 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001770
1771 // Fall though for the Identifier case that is not a register or a
1772 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00001773 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00001774 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1775 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001776 // This was not a register so parse other operands that start with an
1777 // identifier (like labels) as expressions and create them as immediates.
1778 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001779 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001780 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001781 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001782 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001783 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1784 return false;
1785 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001786 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001787 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001788 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001789 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001790 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001791 // #42 -> immediate.
1792 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001793 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001794 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001795 const MCExpr *ImmVal;
1796 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001797 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001798 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001799 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1800 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001801 case AsmToken::Colon: {
1802 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001803 // FIXME: Check it's an expression prefix,
1804 // e.g. (FOO - :lower16:BAR) isn't legal.
1805 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001806 if (ParsePrefix(RefKind))
1807 return true;
1808
Evan Cheng75972122011-01-13 07:58:56 +00001809 const MCExpr *SubExprVal;
1810 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001811 return true;
1812
Evan Cheng75972122011-01-13 07:58:56 +00001813 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1814 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001815 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001816 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001817 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001818 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001819 }
1820}
1821
Evan Cheng75972122011-01-13 07:58:56 +00001822// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1823// :lower16: and :upper16:.
1824bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1825 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001826
1827 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001828 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001829 Parser.Lex(); // Eat ':'
1830
1831 if (getLexer().isNot(AsmToken::Identifier)) {
1832 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1833 return true;
1834 }
1835
1836 StringRef IDVal = Parser.getTok().getIdentifier();
1837 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001838 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001839 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001840 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001841 } else {
1842 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1843 return true;
1844 }
1845 Parser.Lex();
1846
1847 if (getLexer().isNot(AsmToken::Colon)) {
1848 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1849 return true;
1850 }
1851 Parser.Lex(); // Eat the last ':'
1852 return false;
1853}
1854
1855const MCExpr *
1856ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1857 MCSymbolRefExpr::VariantKind Variant) {
1858 // Recurse over the given expression, rebuilding it to apply the given variant
1859 // to the leftmost symbol.
1860 if (Variant == MCSymbolRefExpr::VK_None)
1861 return E;
1862
1863 switch (E->getKind()) {
1864 case MCExpr::Target:
1865 llvm_unreachable("Can't handle target expr yet");
1866 case MCExpr::Constant:
1867 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1868
1869 case MCExpr::SymbolRef: {
1870 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1871
1872 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1873 return 0;
1874
1875 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1876 }
1877
1878 case MCExpr::Unary:
1879 llvm_unreachable("Can't handle unary expressions yet");
1880
1881 case MCExpr::Binary: {
1882 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1883 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1884 const MCExpr *RHS = BE->getRHS();
1885 if (!LHS)
1886 return 0;
1887
1888 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1889 }
1890 }
1891
1892 assert(0 && "Invalid expression kind!");
1893 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001894}
1895
Daniel Dunbar352e1482011-01-11 15:59:50 +00001896/// \brief Given a mnemonic, split out possible predication code and carry
1897/// setting letters to form a canonical mnemonic and flags.
1898//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001899// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001900static StringRef SplitMnemonic(StringRef Mnemonic,
1901 unsigned &PredicationCode,
1902 bool &CarrySetting,
1903 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001904 PredicationCode = ARMCC::AL;
1905 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001906 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001907
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001908 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001909 //
1910 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001911 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1912 Mnemonic == "movs" ||
1913 Mnemonic == "svc" ||
1914 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1915 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1916 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1917 Mnemonic == "vclt" ||
1918 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1919 Mnemonic == "vcle" ||
1920 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1921 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbachd1f0bbe2011-06-27 20:59:10 +00001922 Mnemonic == "vqdmlal" || Mnemonic == "bics"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001923 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001924
Jim Grosbach3f00e312011-07-11 17:09:57 +00001925 // First, split out any predication code. Ignore mnemonics we know aren't
1926 // predicated but do have a carry-set and so weren't caught above.
1927 if (Mnemonic != "adcs") {
1928 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1929 .Case("eq", ARMCC::EQ)
1930 .Case("ne", ARMCC::NE)
1931 .Case("hs", ARMCC::HS)
1932 .Case("cs", ARMCC::HS)
1933 .Case("lo", ARMCC::LO)
1934 .Case("cc", ARMCC::LO)
1935 .Case("mi", ARMCC::MI)
1936 .Case("pl", ARMCC::PL)
1937 .Case("vs", ARMCC::VS)
1938 .Case("vc", ARMCC::VC)
1939 .Case("hi", ARMCC::HI)
1940 .Case("ls", ARMCC::LS)
1941 .Case("ge", ARMCC::GE)
1942 .Case("lt", ARMCC::LT)
1943 .Case("gt", ARMCC::GT)
1944 .Case("le", ARMCC::LE)
1945 .Case("al", ARMCC::AL)
1946 .Default(~0U);
1947 if (CC != ~0U) {
1948 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
1949 PredicationCode = CC;
1950 }
Bill Wendling52925b62010-10-29 23:50:21 +00001951 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001952
Daniel Dunbar352e1482011-01-11 15:59:50 +00001953 // Next, determine if we have a carry setting bit. We explicitly ignore all
1954 // the instructions we know end in 's'.
1955 if (Mnemonic.endswith("s") &&
1956 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1957 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1958 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1959 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1960 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1961 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1962 CarrySetting = true;
1963 }
1964
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001965 // The "cps" instruction can have a interrupt mode operand which is glued into
1966 // the mnemonic. Check if this is the case, split it and parse the imod op
1967 if (Mnemonic.startswith("cps")) {
1968 // Split out any imod code.
1969 unsigned IMod =
1970 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1971 .Case("ie", ARM_PROC::IE)
1972 .Case("id", ARM_PROC::ID)
1973 .Default(~0U);
1974 if (IMod != ~0U) {
1975 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1976 ProcessorIMod = IMod;
1977 }
1978 }
1979
Daniel Dunbar352e1482011-01-11 15:59:50 +00001980 return Mnemonic;
1981}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001982
1983/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1984/// inclusion of carry set or predication code operands.
1985//
1986// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001987void ARMAsmParser::
1988GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1989 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001990 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1991 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1992 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1993 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001994 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001995 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1996 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001997 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00001998 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001999 CanAcceptCarrySet = true;
2000 } else {
2001 CanAcceptCarrySet = false;
2002 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002003
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002004 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2005 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2006 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2007 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00002008 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002009 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002010 CanAcceptPredicationCode = false;
2011 } else {
2012 CanAcceptPredicationCode = true;
2013 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002014
Evan Chengebdeeab2011-07-08 01:53:10 +00002015 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002016 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002017 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002018 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002019}
2020
2021/// Parse an arm instruction mnemonic followed by its operands.
2022bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2023 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2024 // Create the leading tokens for the mnemonic, split by '.' characters.
2025 size_t Start = 0, Next = Name.find('.');
2026 StringRef Head = Name.slice(Start, Next);
2027
Daniel Dunbar352e1482011-01-11 15:59:50 +00002028 // Split out the predication code and carry setting flag from the mnemonic.
2029 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002030 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002031 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002032 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
2033 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002034
Chris Lattner3a697562010-10-28 17:20:03 +00002035 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00002036
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002037 // Next, add the CCOut and ConditionCode operands, if needed.
2038 //
2039 // For mnemonics which can ever incorporate a carry setting bit or predication
2040 // code, our matching model involves us always generating CCOut and
2041 // ConditionCode operands to match the mnemonic "as written" and then we let
2042 // the matcher deal with finding the right instruction or generating an
2043 // appropriate error.
2044 bool CanAcceptCarrySet, CanAcceptPredicationCode;
2045 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
2046
2047 // Add the carry setting operand, if necessary.
2048 //
2049 // FIXME: It would be awesome if we could somehow invent a location such that
2050 // match errors on this operand would print a nice diagnostic about how the
2051 // 's' character in the mnemonic resulted in a CCOut operand.
2052 if (CanAcceptCarrySet) {
2053 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2054 NameLoc));
2055 } else {
2056 // This mnemonic can't ever accept a carry set, but the user wrote one (or
2057 // misspelled another mnemonic).
2058
2059 // FIXME: Issue a nice error.
2060 }
2061
2062 // Add the predication code operand, if necessary.
2063 if (CanAcceptPredicationCode) {
2064 Operands.push_back(ARMOperand::CreateCondCode(
2065 ARMCC::CondCodes(PredicationCode), NameLoc));
2066 } else {
2067 // This mnemonic can't ever accept a predication code, but the user wrote
2068 // one (or misspelled another mnemonic).
2069
2070 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002071 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002072
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002073 // Add the processor imod operand, if necessary.
2074 if (ProcessorIMod) {
2075 Operands.push_back(ARMOperand::CreateImm(
2076 MCConstantExpr::Create(ProcessorIMod, getContext()),
2077 NameLoc, NameLoc));
2078 } else {
2079 // This mnemonic can't ever accept a imod, but the user wrote
2080 // one (or misspelled another mnemonic).
2081
2082 // FIXME: Issue a nice error.
2083 }
2084
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002085 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002086 while (Next != StringRef::npos) {
2087 Start = Next;
2088 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002089 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002090
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002091 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002092 }
2093
2094 // Read the remaining operands.
2095 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002096 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002097 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002098 Parser.EatToEndOfStatement();
2099 return true;
2100 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002101
2102 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002103 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002104
2105 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002106 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002107 Parser.EatToEndOfStatement();
2108 return true;
2109 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002110 }
2111 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002112
Chris Lattnercbf8a982010-09-11 16:18:25 +00002113 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2114 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002115 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002116 }
Bill Wendling146018f2010-11-06 21:42:12 +00002117
Chris Lattner34e53142010-09-08 05:10:46 +00002118 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00002119 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002120}
2121
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002122bool ARMAsmParser::
2123MatchAndEmitInstruction(SMLoc IDLoc,
2124 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2125 MCStreamer &Out) {
2126 MCInst Inst;
2127 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002128 MatchResultTy MatchResult, MatchResult2;
2129 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2130 if (MatchResult != Match_Success) {
2131 // If we get a Match_InvalidOperand it might be some arithmetic instruction
2132 // that does not update the condition codes. So try adding a CCOut operand
2133 // with a value of reg0.
2134 if (MatchResult == Match_InvalidOperand) {
2135 Operands.insert(Operands.begin() + 1,
2136 ARMOperand::CreateCCOut(0,
2137 ((ARMOperand*)Operands[0])->getStartLoc()));
2138 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2139 if (MatchResult2 == Match_Success)
2140 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002141 else {
2142 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002143 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002144 delete CCOut;
2145 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002146 }
2147 // If we get a Match_MnemonicFail it might be some arithmetic instruction
2148 // that updates the condition codes if it ends in 's'. So see if the
2149 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
2150 // operand with a value of CPSR.
Evan Chengeb0caa12011-07-08 22:49:55 +00002151 else if (MatchResult == Match_MnemonicFail) {
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002152 // Get the instruction mnemonic, which is the first token.
2153 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
2154 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
2155 // removed the 's' from the mnemonic for matching.
2156 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
2157 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002158 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2159 Operands.erase(Operands.begin());
2160 delete OldMnemonic;
2161 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002162 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
2163 Operands.insert(Operands.begin() + 1,
2164 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2165 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2166 if (MatchResult2 == Match_Success)
2167 MatchResult = Match_Success;
2168 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002169 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2170 Operands.erase(Operands.begin());
2171 delete OldMnemonic;
2172 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002173 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002174 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2175 Operands.erase(Operands.begin() + 1);
2176 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002177 }
2178 }
2179 }
2180 }
2181 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002182 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002183 Out.EmitInstruction(Inst);
2184 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002185 case Match_MissingFeature:
2186 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2187 return true;
2188 case Match_InvalidOperand: {
2189 SMLoc ErrorLoc = IDLoc;
2190 if (ErrorInfo != ~0U) {
2191 if (ErrorInfo >= Operands.size())
2192 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002193
Chris Lattnere73d4f82010-10-28 21:41:58 +00002194 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2195 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2196 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002197
Chris Lattnere73d4f82010-10-28 21:41:58 +00002198 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002199 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002200 case Match_MnemonicFail:
2201 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002202 case Match_ConversionFail:
2203 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002204 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002205
Eric Christopherc223e2b2010-10-29 09:26:59 +00002206 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002207 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002208}
2209
Kevin Enderby515d5092009-10-15 20:48:48 +00002210/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002211bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2212 StringRef IDVal = DirectiveID.getIdentifier();
2213 if (IDVal == ".word")
2214 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002215 else if (IDVal == ".thumb")
2216 return ParseDirectiveThumb(DirectiveID.getLoc());
2217 else if (IDVal == ".thumb_func")
2218 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2219 else if (IDVal == ".code")
2220 return ParseDirectiveCode(DirectiveID.getLoc());
2221 else if (IDVal == ".syntax")
2222 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002223 return true;
2224}
2225
2226/// ParseDirectiveWord
2227/// ::= .word [ expression (, expression)* ]
2228bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2229 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2230 for (;;) {
2231 const MCExpr *Value;
2232 if (getParser().ParseExpression(Value))
2233 return true;
2234
Chris Lattneraaec2052010-01-19 19:46:13 +00002235 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002236
2237 if (getLexer().is(AsmToken::EndOfStatement))
2238 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002239
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002240 // FIXME: Improve diagnostic.
2241 if (getLexer().isNot(AsmToken::Comma))
2242 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002243 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002244 }
2245 }
2246
Sean Callananb9a25b72010-01-19 20:27:46 +00002247 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002248 return false;
2249}
2250
Kevin Enderby515d5092009-10-15 20:48:48 +00002251/// ParseDirectiveThumb
2252/// ::= .thumb
2253bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2254 if (getLexer().isNot(AsmToken::EndOfStatement))
2255 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002256 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002257
2258 // TODO: set thumb mode
2259 // TODO: tell the MC streamer the mode
2260 // getParser().getStreamer().Emit???();
2261 return false;
2262}
2263
2264/// ParseDirectiveThumbFunc
2265/// ::= .thumbfunc symbol_name
2266bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002267 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2268 bool isMachO = MAI.hasSubsectionsViaSymbols();
2269 StringRef Name;
2270
2271 // Darwin asm has function name after .thumb_func direction
2272 // ELF doesn't
2273 if (isMachO) {
2274 const AsmToken &Tok = Parser.getTok();
2275 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2276 return Error(L, "unexpected token in .thumb_func directive");
2277 Name = Tok.getString();
2278 Parser.Lex(); // Consume the identifier token.
2279 }
2280
Kevin Enderby515d5092009-10-15 20:48:48 +00002281 if (getLexer().isNot(AsmToken::EndOfStatement))
2282 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002283 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002284
Rafael Espindola64695402011-05-16 16:17:21 +00002285 // FIXME: assuming function name will be the line following .thumb_func
2286 if (!isMachO) {
2287 Name = Parser.getTok().getString();
2288 }
2289
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002290 // Mark symbol as a thumb symbol.
2291 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2292 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002293 return false;
2294}
2295
2296/// ParseDirectiveSyntax
2297/// ::= .syntax unified | divided
2298bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002299 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002300 if (Tok.isNot(AsmToken::Identifier))
2301 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002302 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002303 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002304 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002305 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002306 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002307 else
2308 return Error(L, "unrecognized syntax mode in .syntax directive");
2309
2310 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002311 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002312 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002313
2314 // TODO tell the MC streamer the mode
2315 // getParser().getStreamer().Emit???();
2316 return false;
2317}
2318
2319/// ParseDirectiveCode
2320/// ::= .code 16 | 32
2321bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002322 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002323 if (Tok.isNot(AsmToken::Integer))
2324 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002325 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002326 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002327 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002328 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002329 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002330 else
2331 return Error(L, "invalid operand to .code directive");
2332
2333 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002334 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002335 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002336
Evan Cheng32869202011-07-08 22:36:29 +00002337 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002338 if (!isThumb())
2339 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002340 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002341 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002342 if (isThumb())
2343 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002344 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002345 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002346
Kevin Enderby515d5092009-10-15 20:48:48 +00002347 return false;
2348}
2349
Sean Callanan90b70972010-04-07 20:29:34 +00002350extern "C" void LLVMInitializeARMAsmLexer();
2351
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002352/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002353extern "C" void LLVMInitializeARMAsmParser() {
2354 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2355 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002356 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002357}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002358
Chris Lattner0692ee62010-09-06 19:11:01 +00002359#define GET_REGISTER_MATCHER
2360#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002361#include "ARMGenAsmMatcher.inc"