blob: 035907dd0a5196d9d071a4f9307cc4e8dacc2f5f [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*> &);
Owen Anderson00828302011-03-18 22:50:18 +000056 bool TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000057 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000058 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
59 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000060 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000061 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000062 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
63 MCSymbolRefExpr::VariantKind Variant);
64
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000065
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 bool ParseMemoryOffsetReg(bool &Negative,
67 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000068 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000069 const MCExpr *&ShiftAmount,
70 const MCExpr *&Offset,
71 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000072 int &OffsetRegNum,
73 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000074 bool ParseShift(enum ARM_AM::ShiftOpc &St,
75 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000076 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000078 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000080 bool ParseDirectiveSyntax(SMLoc L);
81
Chris Lattner7036f8b2010-09-29 01:42:58 +000082 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000083 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000084 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000085 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
86 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000087
Evan Chengebdeeab2011-07-08 01:53:10 +000088 bool isThumb() const {
89 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000090 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000091 }
Evan Chengebdeeab2011-07-08 01:53:10 +000092 bool isThumbOne() const {
Evan Chengffc0e732011-07-09 05:47:46 +000093 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000094 }
Evan Cheng32869202011-07-08 22:36:29 +000095 void SwitchMode() {
Evan Chengffc0e732011-07-09 05:47:46 +000096 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
97 setAvailableFeatures(FB);
Evan Cheng32869202011-07-08 22:36:29 +000098 }
Evan Chengebdeeab2011-07-08 01:53:10 +000099
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000100 /// @name Auto-generated Match Functions
101 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000102
Chris Lattner0692ee62010-09-06 19:11:01 +0000103#define GET_ASSEMBLER_HEADER
104#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000105
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000106 /// }
107
Jim Grosbachf922c472011-02-12 01:34:40 +0000108 OperandMatchResultTy tryParseCoprocNumOperand(
109 SmallVectorImpl<MCParsedAsmOperand*>&);
110 OperandMatchResultTy tryParseCoprocRegOperand(
111 SmallVectorImpl<MCParsedAsmOperand*>&);
112 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000113 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000114 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000115 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000116 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000117 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000118 OperandMatchResultTy tryParseMemMode2Operand(
119 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000120 OperandMatchResultTy tryParseMemMode3Operand(
121 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000122
123 // Asm Match Converter Methods
124 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
125 const SmallVectorImpl<MCParsedAsmOperand*> &);
126 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
127 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000128 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
129 const SmallVectorImpl<MCParsedAsmOperand*> &);
130 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
131 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000132
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000133public:
Evan Chengffc0e732011-07-09 05:47:46 +0000134 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
135 : TargetAsmParser(), STI(_STI), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000136 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000137
Evan Chengebdeeab2011-07-08 01:53:10 +0000138 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000139 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evan Chengebdeeab2011-07-08 01:53:10 +0000140 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000141
Benjamin Kramer38e59892010-07-14 22:38:02 +0000142 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000143 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000144 virtual bool ParseDirective(AsmToken DirectiveID);
145};
Jim Grosbach16c74252010-10-29 14:46:02 +0000146} // end anonymous namespace
147
Chris Lattner3a697562010-10-28 17:20:03 +0000148namespace {
149
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000150/// ARMOperand - Instances of this class represent a parsed ARM machine
151/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000152class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000153 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000154 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000155 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000156 CoprocNum,
157 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000158 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000159 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000160 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000161 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000162 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000163 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000164 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000165 DPRRegisterList,
166 SPRRegisterList,
Jim Grosbache8606dc2011-07-13 17:50:29 +0000167 ShiftedRegister,
Owen Anderson00828302011-03-18 22:50:18 +0000168 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000169 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000170 } Kind;
171
Sean Callanan76264762010-04-02 22:27:05 +0000172 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000173 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000174
175 union {
176 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000177 ARMCC::CondCodes Val;
178 } CC;
179
180 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000181 ARM_MB::MemBOpt Val;
182 } MBOpt;
183
184 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000185 unsigned Val;
186 } Cop;
187
188 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000189 ARM_PROC::IFlags Val;
190 } IFlags;
191
192 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000193 unsigned Val;
194 } MMask;
195
196 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000197 const char *Data;
198 unsigned Length;
199 } Tok;
200
201 struct {
202 unsigned RegNum;
203 } Reg;
204
Bill Wendling8155e5b2010-11-06 22:19:43 +0000205 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000206 const MCExpr *Val;
207 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000208
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000209 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000210 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000211 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000212 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000213 union {
214 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
215 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
216 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000217 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000218 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000219 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000220 unsigned Preindexed : 1;
221 unsigned Postindexed : 1;
222 unsigned OffsetIsReg : 1;
223 unsigned Negative : 1; // only used when OffsetIsReg is true
224 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000225 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000226
227 struct {
228 ARM_AM::ShiftOpc ShiftTy;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000229 unsigned Imm;
Owen Anderson00828302011-03-18 22:50:18 +0000230 } Shift;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000231 struct {
232 ARM_AM::ShiftOpc ShiftTy;
233 unsigned SrcReg;
234 unsigned ShiftReg;
235 unsigned ShiftImm;
236 } ShiftedReg;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000237 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000238
Bill Wendling146018f2010-11-06 21:42:12 +0000239 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
240public:
Sean Callanan76264762010-04-02 22:27:05 +0000241 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
242 Kind = o.Kind;
243 StartLoc = o.StartLoc;
244 EndLoc = o.EndLoc;
245 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000246 case CondCode:
247 CC = o.CC;
248 break;
Sean Callanan76264762010-04-02 22:27:05 +0000249 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000250 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000251 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000252 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000253 case Register:
254 Reg = o.Reg;
255 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000256 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000257 case DPRRegisterList:
258 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000259 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000260 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000261 case CoprocNum:
262 case CoprocReg:
263 Cop = o.Cop;
264 break;
Sean Callanan76264762010-04-02 22:27:05 +0000265 case Immediate:
266 Imm = o.Imm;
267 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000268 case MemBarrierOpt:
269 MBOpt = o.MBOpt;
270 break;
Sean Callanan76264762010-04-02 22:27:05 +0000271 case Memory:
272 Mem = o.Mem;
273 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000274 case MSRMask:
275 MMask = o.MMask;
276 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000277 case ProcIFlags:
278 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000279 break;
280 case Shifter:
281 Shift = o.Shift;
282 break;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000283 case ShiftedRegister:
284 ShiftedReg = o.ShiftedReg;
285 break;
Sean Callanan76264762010-04-02 22:27:05 +0000286 }
287 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000288
Sean Callanan76264762010-04-02 22:27:05 +0000289 /// getStartLoc - Get the location of the first token of this operand.
290 SMLoc getStartLoc() const { return StartLoc; }
291 /// getEndLoc - Get the location of the last token of this operand.
292 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000293
Daniel Dunbar8462b302010-08-11 06:36:53 +0000294 ARMCC::CondCodes getCondCode() const {
295 assert(Kind == CondCode && "Invalid access!");
296 return CC.Val;
297 }
298
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000299 unsigned getCoproc() const {
300 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
301 return Cop.Val;
302 }
303
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000304 StringRef getToken() const {
305 assert(Kind == Token && "Invalid access!");
306 return StringRef(Tok.Data, Tok.Length);
307 }
308
309 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000310 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000311 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000312 }
313
Bill Wendling5fa22a12010-11-09 23:28:44 +0000314 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000315 assert((Kind == RegisterList || Kind == DPRRegisterList ||
316 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000317 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000318 }
319
Kevin Enderbycfe07242009-10-13 22:19:02 +0000320 const MCExpr *getImm() const {
321 assert(Kind == Immediate && "Invalid access!");
322 return Imm.Val;
323 }
324
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000325 ARM_MB::MemBOpt getMemBarrierOpt() const {
326 assert(Kind == MemBarrierOpt && "Invalid access!");
327 return MBOpt.Val;
328 }
329
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000330 ARM_PROC::IFlags getProcIFlags() const {
331 assert(Kind == ProcIFlags && "Invalid access!");
332 return IFlags.Val;
333 }
334
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000335 unsigned getMSRMask() const {
336 assert(Kind == MSRMask && "Invalid access!");
337 return MMask.Val;
338 }
339
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000340 /// @name Memory Operand Accessors
341 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000342 ARMII::AddrMode getMemAddrMode() const {
343 return Mem.AddrMode;
344 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000345 unsigned getMemBaseRegNum() const {
346 return Mem.BaseRegNum;
347 }
348 unsigned getMemOffsetRegNum() const {
349 assert(Mem.OffsetIsReg && "Invalid access!");
350 return Mem.Offset.RegNum;
351 }
352 const MCExpr *getMemOffset() const {
353 assert(!Mem.OffsetIsReg && "Invalid access!");
354 return Mem.Offset.Value;
355 }
356 unsigned getMemOffsetRegShifted() const {
357 assert(Mem.OffsetIsReg && "Invalid access!");
358 return Mem.OffsetRegShifted;
359 }
360 const MCExpr *getMemShiftAmount() const {
361 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
362 return Mem.ShiftAmount;
363 }
Owen Anderson00828302011-03-18 22:50:18 +0000364 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000365 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
366 return Mem.ShiftType;
367 }
368 bool getMemPreindexed() const { return Mem.Preindexed; }
369 bool getMemPostindexed() const { return Mem.Postindexed; }
370 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
371 bool getMemNegative() const { return Mem.Negative; }
372 bool getMemWriteback() const { return Mem.Writeback; }
373
374 /// @}
375
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000376 bool isCoprocNum() const { return Kind == CoprocNum; }
377 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000378 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000379 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000380 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000381 bool isImm0_255() const {
382 if (Kind != Immediate)
383 return false;
384 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
385 if (!CE) return false;
386 int64_t Value = CE->getValue();
387 return Value >= 0 && Value < 256;
388 }
389 bool isT2SOImm() const {
390 if (Kind != Immediate)
391 return false;
392 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
393 if (!CE) return false;
394 int64_t Value = CE->getValue();
395 return ARM_AM::getT2SOImmVal(Value) != -1;
396 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000397 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000398 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000399 bool isDPRRegList() const { return Kind == DPRRegisterList; }
400 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000401 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000402 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000403 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000404 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000405 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000406 bool isMemMode2() const {
407 if (getMemAddrMode() != ARMII::AddrMode2)
408 return false;
409
410 if (getMemOffsetIsReg())
411 return true;
412
413 if (getMemNegative() &&
414 !(getMemPostindexed() || getMemPreindexed()))
415 return false;
416
417 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
418 if (!CE) return false;
419 int64_t Value = CE->getValue();
420
421 // The offset must be in the range 0-4095 (imm12).
422 if (Value > 4095 || Value < -4095)
423 return false;
424
425 return true;
426 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000427 bool isMemMode3() const {
428 if (getMemAddrMode() != ARMII::AddrMode3)
429 return false;
430
431 if (getMemOffsetIsReg()) {
432 if (getMemOffsetRegShifted())
433 return false; // No shift with offset reg allowed
434 return true;
435 }
436
437 if (getMemNegative() &&
438 !(getMemPostindexed() || getMemPreindexed()))
439 return false;
440
441 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
442 if (!CE) return false;
443 int64_t Value = CE->getValue();
444
445 // The offset must be in the range 0-255 (imm8).
446 if (Value > 255 || Value < -255)
447 return false;
448
449 return true;
450 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000451 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000452 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
453 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000454 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000455
Daniel Dunbar4b462672011-01-18 05:55:27 +0000456 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000457 if (!CE) return false;
458
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000459 // The offset must be a multiple of 4 in the range 0-1020.
460 int64_t Value = CE->getValue();
461 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
462 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000463 bool isMemMode7() const {
464 if (!isMemory() ||
465 getMemPreindexed() ||
466 getMemPostindexed() ||
467 getMemOffsetIsReg() ||
468 getMemNegative() ||
469 getMemWriteback())
470 return false;
471
472 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
473 if (!CE) return false;
474
475 if (CE->getValue())
476 return false;
477
478 return true;
479 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000480 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000481 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000482 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000483 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000484 }
485 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000486 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000487 return false;
488
Daniel Dunbar4b462672011-01-18 05:55:27 +0000489 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000490 if (!CE) return false;
491
492 // The offset must be a multiple of 4 in the range 0-124.
493 uint64_t Value = CE->getValue();
494 return ((Value & 0x3) == 0 && Value <= 124);
495 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000496 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000497 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000498
499 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000500 // Add as immediates when possible. Null MCExpr = 0.
501 if (Expr == 0)
502 Inst.addOperand(MCOperand::CreateImm(0));
503 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000504 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
505 else
506 Inst.addOperand(MCOperand::CreateExpr(Expr));
507 }
508
Daniel Dunbar8462b302010-08-11 06:36:53 +0000509 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000510 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000511 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000512 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
513 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000514 }
515
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000516 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
517 assert(N == 1 && "Invalid number of operands!");
518 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
519 }
520
521 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
522 assert(N == 1 && "Invalid number of operands!");
523 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
524 }
525
Jim Grosbachd67641b2010-12-06 18:21:12 +0000526 void addCCOutOperands(MCInst &Inst, unsigned N) const {
527 assert(N == 1 && "Invalid number of operands!");
528 Inst.addOperand(MCOperand::CreateReg(getReg()));
529 }
530
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000531 void addRegOperands(MCInst &Inst, unsigned N) const {
532 assert(N == 1 && "Invalid number of operands!");
533 Inst.addOperand(MCOperand::CreateReg(getReg()));
534 }
535
Jim Grosbache8606dc2011-07-13 17:50:29 +0000536 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
537 assert(N == 3 && "Invalid number of operands!");
538 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
539 assert((ShiftedReg.ShiftReg == 0 ||
540 ARM_AM::getSORegOffset(ShiftedReg.ShiftImm) == 0) &&
541 "Invalid shifted register operand!");
542 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
543 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
544 Inst.addOperand(MCOperand::CreateImm(
545 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
546 }
547
Owen Anderson00828302011-03-18 22:50:18 +0000548 void addShifterOperands(MCInst &Inst, unsigned N) const {
549 assert(N == 1 && "Invalid number of operands!");
550 Inst.addOperand(MCOperand::CreateImm(
551 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
552 }
553
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000554 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000555 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000556 const SmallVectorImpl<unsigned> &RegList = getRegList();
557 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000558 I = RegList.begin(), E = RegList.end(); I != E; ++I)
559 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000560 }
561
Bill Wendling0f630752010-11-17 04:32:08 +0000562 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
563 addRegListOperands(Inst, N);
564 }
565
566 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
567 addRegListOperands(Inst, N);
568 }
569
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000570 void addImmOperands(MCInst &Inst, unsigned N) const {
571 assert(N == 1 && "Invalid number of operands!");
572 addExpr(Inst, getImm());
573 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000574
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000575 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
576 assert(N == 1 && "Invalid number of operands!");
577 addExpr(Inst, getImm());
578 }
579
580 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
581 assert(N == 1 && "Invalid number of operands!");
582 addExpr(Inst, getImm());
583 }
584
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000585 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
586 assert(N == 1 && "Invalid number of operands!");
587 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
588 }
589
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000590 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
591 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
592 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
593
594 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000595 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000596 assert((CE || CE->getValue() == 0) &&
597 "No offset operand support in mode 7");
598 }
599
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000600 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
601 assert(isMemMode2() && "Invalid mode or number of operands!");
602 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
603 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
604
605 if (getMemOffsetIsReg()) {
606 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
607
608 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
609 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
610 int64_t ShiftAmount = 0;
611
612 if (getMemOffsetRegShifted()) {
613 ShOpc = getMemShiftType();
614 const MCConstantExpr *CE =
615 dyn_cast<MCConstantExpr>(getMemShiftAmount());
616 ShiftAmount = CE->getValue();
617 }
618
619 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
620 ShOpc, IdxMode)));
621 return;
622 }
623
624 // Create a operand placeholder to always yield the same number of operands.
625 Inst.addOperand(MCOperand::CreateReg(0));
626
627 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
628 // the difference?
629 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
630 assert(CE && "Non-constant mode 2 offset operand!");
631 int64_t Offset = CE->getValue();
632
633 if (Offset >= 0)
634 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
635 Offset, ARM_AM::no_shift, IdxMode)));
636 else
637 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
638 -Offset, ARM_AM::no_shift, IdxMode)));
639 }
640
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000641 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
642 assert(isMemMode3() && "Invalid mode or number of operands!");
643 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
644 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
645
646 if (getMemOffsetIsReg()) {
647 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
648
649 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
650 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
651 IdxMode)));
652 return;
653 }
654
655 // Create a operand placeholder to always yield the same number of operands.
656 Inst.addOperand(MCOperand::CreateReg(0));
657
658 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
659 // the difference?
660 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
661 assert(CE && "Non-constant mode 3 offset operand!");
662 int64_t Offset = CE->getValue();
663
664 if (Offset >= 0)
665 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
666 Offset, IdxMode)));
667 else
668 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
669 -Offset, IdxMode)));
670 }
671
Chris Lattner14b93852010-10-29 00:27:31 +0000672 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
673 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000674
Daniel Dunbar4b462672011-01-18 05:55:27 +0000675 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
676 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000677
Jim Grosbach80eb2332010-10-29 17:41:25 +0000678 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
679 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000680 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000681 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000682
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000683 // The MCInst offset operand doesn't include the low two bits (like
684 // the instruction encoding).
685 int64_t Offset = CE->getValue() / 4;
686 if (Offset >= 0)
687 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
688 Offset)));
689 else
690 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
691 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000692 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000693
Bill Wendlingf4caf692010-12-14 03:36:38 +0000694 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
695 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000696 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
697 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000698 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000699
Bill Wendlingf4caf692010-12-14 03:36:38 +0000700 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
701 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000702 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
703 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000704 assert(CE && "Non-constant mode offset operand!");
705 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000706 }
707
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000708 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
709 assert(N == 1 && "Invalid number of operands!");
710 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
711 }
712
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000713 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
714 assert(N == 1 && "Invalid number of operands!");
715 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
716 }
717
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000718 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000719
Chris Lattner3a697562010-10-28 17:20:03 +0000720 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
721 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000722 Op->CC.Val = CC;
723 Op->StartLoc = S;
724 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000725 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000726 }
727
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000728 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
729 ARMOperand *Op = new ARMOperand(CoprocNum);
730 Op->Cop.Val = CopVal;
731 Op->StartLoc = S;
732 Op->EndLoc = S;
733 return Op;
734 }
735
736 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
737 ARMOperand *Op = new ARMOperand(CoprocReg);
738 Op->Cop.Val = CopVal;
739 Op->StartLoc = S;
740 Op->EndLoc = S;
741 return Op;
742 }
743
Jim Grosbachd67641b2010-12-06 18:21:12 +0000744 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
745 ARMOperand *Op = new ARMOperand(CCOut);
746 Op->Reg.RegNum = RegNum;
747 Op->StartLoc = S;
748 Op->EndLoc = S;
749 return Op;
750 }
751
Chris Lattner3a697562010-10-28 17:20:03 +0000752 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
753 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000754 Op->Tok.Data = Str.data();
755 Op->Tok.Length = Str.size();
756 Op->StartLoc = S;
757 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000758 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000759 }
760
Bill Wendling50d0f582010-11-18 23:43:05 +0000761 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000762 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000763 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000764 Op->StartLoc = S;
765 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000766 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000767 }
768
Jim Grosbache8606dc2011-07-13 17:50:29 +0000769 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
770 unsigned SrcReg,
771 unsigned ShiftReg,
772 unsigned ShiftImm,
773 SMLoc S, SMLoc E) {
774 ARMOperand *Op = new ARMOperand(ShiftedRegister);
775 Op->ShiftedReg.ShiftTy = ShTy;
776 Op->ShiftedReg.SrcReg = SrcReg;
777 Op->ShiftedReg.ShiftReg = ShiftReg;
778 Op->ShiftedReg.ShiftImm = ShiftImm;
779 Op->StartLoc = S;
780 Op->EndLoc = E;
781 return Op;
782 }
783
Owen Anderson00828302011-03-18 22:50:18 +0000784 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
785 SMLoc S, SMLoc E) {
786 ARMOperand *Op = new ARMOperand(Shifter);
787 Op->Shift.ShiftTy = ShTy;
788 Op->StartLoc = S;
789 Op->EndLoc = E;
790 return Op;
791 }
792
Bill Wendling7729e062010-11-09 22:44:22 +0000793 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000794 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000795 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000796 KindTy Kind = RegisterList;
797
798 if (ARM::DPRRegClass.contains(Regs.front().first))
799 Kind = DPRRegisterList;
800 else if (ARM::SPRRegClass.contains(Regs.front().first))
801 Kind = SPRRegisterList;
802
803 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000804 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000805 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000806 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000807 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000808 Op->StartLoc = StartLoc;
809 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000810 return Op;
811 }
812
Chris Lattner3a697562010-10-28 17:20:03 +0000813 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
814 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000815 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000816 Op->StartLoc = S;
817 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000818 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000819 }
820
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000821 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
822 bool OffsetIsReg, const MCExpr *Offset,
823 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000824 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000825 const MCExpr *ShiftAmount, bool Preindexed,
826 bool Postindexed, bool Negative, bool Writeback,
827 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000828 assert((OffsetRegNum == -1 || OffsetIsReg) &&
829 "OffsetRegNum must imply OffsetIsReg!");
830 assert((!OffsetRegShifted || OffsetIsReg) &&
831 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000832 assert((Offset || OffsetIsReg) &&
833 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000834 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
835 "Cannot have shift amount without shifted register offset!");
836 assert((!Offset || !OffsetIsReg) &&
837 "Cannot have expression offset and register offset!");
838
Chris Lattner3a697562010-10-28 17:20:03 +0000839 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000840 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000841 Op->Mem.BaseRegNum = BaseRegNum;
842 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000843 if (OffsetIsReg)
844 Op->Mem.Offset.RegNum = OffsetRegNum;
845 else
846 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000847 Op->Mem.OffsetRegShifted = OffsetRegShifted;
848 Op->Mem.ShiftType = ShiftType;
849 Op->Mem.ShiftAmount = ShiftAmount;
850 Op->Mem.Preindexed = Preindexed;
851 Op->Mem.Postindexed = Postindexed;
852 Op->Mem.Negative = Negative;
853 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000854
Sean Callanan76264762010-04-02 22:27:05 +0000855 Op->StartLoc = S;
856 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000857 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000858 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000859
860 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
861 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
862 Op->MBOpt.Val = Opt;
863 Op->StartLoc = S;
864 Op->EndLoc = S;
865 return Op;
866 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000867
868 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
869 ARMOperand *Op = new ARMOperand(ProcIFlags);
870 Op->IFlags.Val = IFlags;
871 Op->StartLoc = S;
872 Op->EndLoc = S;
873 return Op;
874 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000875
876 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
877 ARMOperand *Op = new ARMOperand(MSRMask);
878 Op->MMask.Val = MMask;
879 Op->StartLoc = S;
880 Op->EndLoc = S;
881 return Op;
882 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000883};
884
885} // end anonymous namespace.
886
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000887void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000888 switch (Kind) {
889 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000890 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000891 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000892 case CCOut:
893 OS << "<ccout " << getReg() << ">";
894 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000895 case CoprocNum:
896 OS << "<coprocessor number: " << getCoproc() << ">";
897 break;
898 case CoprocReg:
899 OS << "<coprocessor register: " << getCoproc() << ">";
900 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000901 case MSRMask:
902 OS << "<mask: " << getMSRMask() << ">";
903 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000904 case Immediate:
905 getImm()->print(OS);
906 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000907 case MemBarrierOpt:
908 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
909 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000910 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000911 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000912 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
913 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000914 if (getMemOffsetIsReg()) {
915 OS << " offset:<register " << getMemOffsetRegNum();
916 if (getMemOffsetRegShifted()) {
917 OS << " offset-shift-type:" << getMemShiftType();
918 OS << " offset-shift-amount:" << *getMemShiftAmount();
919 }
920 } else {
921 OS << " offset:" << *getMemOffset();
922 }
923 if (getMemOffsetIsReg())
924 OS << " (offset-is-reg)";
925 if (getMemPreindexed())
926 OS << " (pre-indexed)";
927 if (getMemPostindexed())
928 OS << " (post-indexed)";
929 if (getMemNegative())
930 OS << " (negative)";
931 if (getMemWriteback())
932 OS << " (writeback)";
933 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000934 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000935 case ProcIFlags: {
936 OS << "<ARM_PROC::";
937 unsigned IFlags = getProcIFlags();
938 for (int i=2; i >= 0; --i)
939 if (IFlags & (1 << i))
940 OS << ARM_PROC::IFlagsToString(1 << i);
941 OS << ">";
942 break;
943 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000944 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000945 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000946 break;
Owen Anderson00828302011-03-18 22:50:18 +0000947 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +0000948 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
949 break;
950 case ShiftedRegister:
951 OS << "<so_reg"
952 << ShiftedReg.SrcReg
953 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
954 << ", " << ShiftedReg.ShiftReg << ", "
955 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
956 << ">";
Owen Anderson00828302011-03-18 22:50:18 +0000957 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000958 case RegisterList:
959 case DPRRegisterList:
960 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000961 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000962
Bill Wendling5fa22a12010-11-09 23:28:44 +0000963 const SmallVectorImpl<unsigned> &RegList = getRegList();
964 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000965 I = RegList.begin(), E = RegList.end(); I != E; ) {
966 OS << *I;
967 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000968 }
969
970 OS << ">";
971 break;
972 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000973 case Token:
974 OS << "'" << getToken() << "'";
975 break;
976 }
977}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000978
979/// @name Auto-generated Match Functions
980/// {
981
982static unsigned MatchRegisterName(StringRef Name);
983
984/// }
985
Bob Wilson69df7232011-02-03 21:46:10 +0000986bool ARMAsmParser::ParseRegister(unsigned &RegNo,
987 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000988 RegNo = TryParseRegister();
989
990 return (RegNo == (unsigned)-1);
991}
992
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000993/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000994/// and if it is a register name the token is eaten and the register number is
995/// returned. Otherwise return -1.
996///
997int ARMAsmParser::TryParseRegister() {
998 const AsmToken &Tok = Parser.getTok();
999 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001000
Chris Lattnere5658fa2010-10-30 04:09:10 +00001001 // FIXME: Validate register for the current architecture; we have to do
1002 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001003 std::string upperCase = Tok.getString().str();
1004 std::string lowerCase = LowercaseString(upperCase);
1005 unsigned RegNum = MatchRegisterName(lowerCase);
1006 if (!RegNum) {
1007 RegNum = StringSwitch<unsigned>(lowerCase)
1008 .Case("r13", ARM::SP)
1009 .Case("r14", ARM::LR)
1010 .Case("r15", ARM::PC)
1011 .Case("ip", ARM::R12)
1012 .Default(0);
1013 }
1014 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001015
Chris Lattnere5658fa2010-10-30 04:09:10 +00001016 Parser.Lex(); // Eat identifier token.
1017 return RegNum;
1018}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001019
Owen Anderson00828302011-03-18 22:50:18 +00001020/// Try to parse a register name. The token must be an Identifier when called,
1021/// and if it is a register name the token is eaten and the register number is
1022/// returned. Otherwise return -1.
1023///
1024bool ARMAsmParser::TryParseShiftRegister(
1025 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1026 SMLoc S = Parser.getTok().getLoc();
1027 const AsmToken &Tok = Parser.getTok();
1028 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1029
1030 std::string upperCase = Tok.getString().str();
1031 std::string lowerCase = LowercaseString(upperCase);
1032 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1033 .Case("lsl", ARM_AM::lsl)
1034 .Case("lsr", ARM_AM::lsr)
1035 .Case("asr", ARM_AM::asr)
1036 .Case("ror", ARM_AM::ror)
1037 .Case("rrx", ARM_AM::rrx)
1038 .Default(ARM_AM::no_shift);
1039
1040 if (ShiftTy == ARM_AM::no_shift)
1041 return true;
1042
Jim Grosbache8606dc2011-07-13 17:50:29 +00001043 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001044
Jim Grosbache8606dc2011-07-13 17:50:29 +00001045 // The source register for the shift has already been added to the
1046 // operand list, so we need to pop it off and combine it into the shifted
1047 // register operand instead.
1048 ARMOperand *PrevOp = (ARMOperand*)Operands.pop_back_val();
1049 if (!PrevOp->isReg())
1050 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1051 int SrcReg = PrevOp->getReg();
1052 int64_t Imm = 0;
1053 int ShiftReg = 0;
1054 if (ShiftTy == ARM_AM::rrx) {
1055 // RRX Doesn't have an explicit shift amount. The encoder expects
1056 // the shift register to be the same as the source register. Seems odd,
1057 // but OK.
1058 ShiftReg = SrcReg;
1059 } else {
1060 // Figure out if this is shifted by a constant or a register (for non-RRX).
1061 if (Parser.getTok().is(AsmToken::Hash)) {
1062 Parser.Lex(); // Eat hash.
1063 SMLoc ImmLoc = Parser.getTok().getLoc();
1064 const MCExpr *ShiftExpr = 0;
1065 if (getParser().ParseExpression(ShiftExpr))
1066 return Error(ImmLoc, "invalid immediate shift value");
1067 // The expression must be evaluatable as an immediate.
1068 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
1069 if (!CE)
1070 return Error(ImmLoc, "invalid immediate shift value");
1071 // Range check the immediate.
1072 // lsl, ror: 0 <= imm <= 31
1073 // lsr, asr: 0 <= imm <= 32
1074 Imm = CE->getValue();
1075 if (Imm < 0 ||
1076 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1077 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
1078 return Error(ImmLoc, "immediate shift value out of range");
1079 }
1080 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1081 ShiftReg = TryParseRegister();
1082 SMLoc L = Parser.getTok().getLoc();
1083 if (ShiftReg == -1)
1084 return Error (L, "expected immediate or register in shift operand");
1085 } else
1086 return Error (Parser.getTok().getLoc(),
1087 "expected immediate or register in shift operand");
1088 }
1089
1090
1091 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1092 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001093 S, Parser.getTok().getLoc()));
1094
1095 return false;
1096}
1097
1098
Bill Wendling50d0f582010-11-18 23:43:05 +00001099/// Try to parse a register name. The token must be an Identifier when called.
1100/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1101/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001102///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001103/// TODO this is likely to change to allow different register types and or to
1104/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001105bool ARMAsmParser::
1106TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001107 SMLoc S = Parser.getTok().getLoc();
1108 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001109 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001110 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001111
Bill Wendling50d0f582010-11-18 23:43:05 +00001112 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001113
Chris Lattnere5658fa2010-10-30 04:09:10 +00001114 const AsmToken &ExclaimTok = Parser.getTok();
1115 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001116 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1117 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001118 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001119 }
1120
Bill Wendling50d0f582010-11-18 23:43:05 +00001121 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001122}
1123
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001124/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1125/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1126/// "c5", ...
1127static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001128 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1129 // but efficient.
1130 switch (Name.size()) {
1131 default: break;
1132 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001133 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001134 return -1;
1135 switch (Name[1]) {
1136 default: return -1;
1137 case '0': return 0;
1138 case '1': return 1;
1139 case '2': return 2;
1140 case '3': return 3;
1141 case '4': return 4;
1142 case '5': return 5;
1143 case '6': return 6;
1144 case '7': return 7;
1145 case '8': return 8;
1146 case '9': return 9;
1147 }
1148 break;
1149 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001150 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001151 return -1;
1152 switch (Name[2]) {
1153 default: return -1;
1154 case '0': return 10;
1155 case '1': return 11;
1156 case '2': return 12;
1157 case '3': return 13;
1158 case '4': return 14;
1159 case '5': return 15;
1160 }
1161 break;
1162 }
1163
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001164 return -1;
1165}
1166
Jim Grosbachf922c472011-02-12 01:34:40 +00001167/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001168/// token must be an Identifier when called, and if it is a coprocessor
1169/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001170ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1171tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001172 SMLoc S = Parser.getTok().getLoc();
1173 const AsmToken &Tok = Parser.getTok();
1174 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1175
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001176 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001177 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001178 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001179
1180 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001181 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001182 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001183}
1184
Jim Grosbachf922c472011-02-12 01:34:40 +00001185/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001186/// token must be an Identifier when called, and if it is a coprocessor
1187/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001188ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1189tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001190 SMLoc S = Parser.getTok().getLoc();
1191 const AsmToken &Tok = Parser.getTok();
1192 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1193
1194 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1195 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001196 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001197
1198 Parser.Lex(); // Eat identifier token.
1199 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001200 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001201}
1202
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001203/// Parse a register list, return it if successful else return null. The first
1204/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001205bool ARMAsmParser::
1206ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001207 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001208 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001209 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001210
Bill Wendling7729e062010-11-09 22:44:22 +00001211 // Read the rest of the registers in the list.
1212 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001213 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001214
Bill Wendling7729e062010-11-09 22:44:22 +00001215 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001216 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001217 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001218
Sean Callanan18b83232010-01-19 21:44:56 +00001219 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001220 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001221 if (RegTok.isNot(AsmToken::Identifier)) {
1222 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001223 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001224 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001225
Bill Wendling1d6a2652010-11-06 10:40:24 +00001226 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001227 if (RegNum == -1) {
1228 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001229 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001230 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001231
Bill Wendlinge7176102010-11-06 22:36:58 +00001232 if (IsRange) {
1233 int Reg = PrevRegNum;
1234 do {
1235 ++Reg;
1236 Registers.push_back(std::make_pair(Reg, RegLoc));
1237 } while (Reg != RegNum);
1238 } else {
1239 Registers.push_back(std::make_pair(RegNum, RegLoc));
1240 }
1241
1242 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001243 } while (Parser.getTok().is(AsmToken::Comma) ||
1244 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001245
1246 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001247 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001248 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1249 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001250 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001251 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001252
Bill Wendlinge7176102010-11-06 22:36:58 +00001253 SMLoc E = RCurlyTok.getLoc();
1254 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001255
Bill Wendlinge7176102010-11-06 22:36:58 +00001256 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001257 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001258 RI = Registers.begin(), RE = Registers.end();
1259
Bill Wendling7caebff2011-01-12 21:20:59 +00001260 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001261 bool EmittedWarning = false;
1262
Bill Wendling7caebff2011-01-12 21:20:59 +00001263 DenseMap<unsigned, bool> RegMap;
1264 RegMap[HighRegNum] = true;
1265
Bill Wendlinge7176102010-11-06 22:36:58 +00001266 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001267 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001268 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001269
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001270 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001271 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001272 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001273 }
1274
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001275 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001276 Warning(RegInfo.second,
1277 "register not in ascending order in register list");
1278
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001279 RegMap[Reg] = true;
1280 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001281 }
1282
Bill Wendling50d0f582010-11-18 23:43:05 +00001283 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1284 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001285}
1286
Jim Grosbachf922c472011-02-12 01:34:40 +00001287/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1288ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1289tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001290 SMLoc S = Parser.getTok().getLoc();
1291 const AsmToken &Tok = Parser.getTok();
1292 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1293 StringRef OptStr = Tok.getString();
1294
1295 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1296 .Case("sy", ARM_MB::SY)
1297 .Case("st", ARM_MB::ST)
1298 .Case("ish", ARM_MB::ISH)
1299 .Case("ishst", ARM_MB::ISHST)
1300 .Case("nsh", ARM_MB::NSH)
1301 .Case("nshst", ARM_MB::NSHST)
1302 .Case("osh", ARM_MB::OSH)
1303 .Case("oshst", ARM_MB::OSHST)
1304 .Default(~0U);
1305
1306 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001307 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001308
1309 Parser.Lex(); // Eat identifier token.
1310 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001311 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001312}
1313
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001314/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001315ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1316tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1317 SMLoc S = Parser.getTok().getLoc();
1318 const AsmToken &Tok = Parser.getTok();
1319 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1320 StringRef IFlagsStr = Tok.getString();
1321
1322 unsigned IFlags = 0;
1323 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1324 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1325 .Case("a", ARM_PROC::A)
1326 .Case("i", ARM_PROC::I)
1327 .Case("f", ARM_PROC::F)
1328 .Default(~0U);
1329
1330 // If some specific iflag is already set, it means that some letter is
1331 // present more than once, this is not acceptable.
1332 if (Flag == ~0U || (IFlags & Flag))
1333 return MatchOperand_NoMatch;
1334
1335 IFlags |= Flag;
1336 }
1337
1338 Parser.Lex(); // Eat identifier token.
1339 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1340 return MatchOperand_Success;
1341}
1342
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001343/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1344ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1345tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1346 SMLoc S = Parser.getTok().getLoc();
1347 const AsmToken &Tok = Parser.getTok();
1348 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1349 StringRef Mask = Tok.getString();
1350
1351 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1352 size_t Start = 0, Next = Mask.find('_');
1353 StringRef Flags = "";
1354 StringRef SpecReg = Mask.slice(Start, Next);
1355 if (Next != StringRef::npos)
1356 Flags = Mask.slice(Next+1, Mask.size());
1357
1358 // FlagsVal contains the complete mask:
1359 // 3-0: Mask
1360 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1361 unsigned FlagsVal = 0;
1362
1363 if (SpecReg == "apsr") {
1364 FlagsVal = StringSwitch<unsigned>(Flags)
1365 .Case("nzcvq", 0x8) // same as CPSR_c
1366 .Case("g", 0x4) // same as CPSR_s
1367 .Case("nzcvqg", 0xc) // same as CPSR_fs
1368 .Default(~0U);
1369
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001370 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001371 if (!Flags.empty())
1372 return MatchOperand_NoMatch;
1373 else
1374 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001375 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001376 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001377 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1378 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001379 for (int i = 0, e = Flags.size(); i != e; ++i) {
1380 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1381 .Case("c", 1)
1382 .Case("x", 2)
1383 .Case("s", 4)
1384 .Case("f", 8)
1385 .Default(~0U);
1386
1387 // If some specific flag is already set, it means that some letter is
1388 // present more than once, this is not acceptable.
1389 if (FlagsVal == ~0U || (FlagsVal & Flag))
1390 return MatchOperand_NoMatch;
1391 FlagsVal |= Flag;
1392 }
1393 } else // No match for special register.
1394 return MatchOperand_NoMatch;
1395
1396 // Special register without flags are equivalent to "fc" flags.
1397 if (!FlagsVal)
1398 FlagsVal = 0x9;
1399
1400 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1401 if (SpecReg == "spsr")
1402 FlagsVal |= 16;
1403
1404 Parser.Lex(); // Eat identifier token.
1405 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1406 return MatchOperand_Success;
1407}
1408
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001409/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1410ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1411tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001412 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001413
1414 if (ParseMemory(Operands, ARMII::AddrMode2))
1415 return MatchOperand_NoMatch;
1416
1417 return MatchOperand_Success;
1418}
1419
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001420/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1421ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1422tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1423 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1424
1425 if (ParseMemory(Operands, ARMII::AddrMode3))
1426 return MatchOperand_NoMatch;
1427
1428 return MatchOperand_Success;
1429}
1430
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001431/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1432/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1433/// when they refer multiple MIOperands inside a single one.
1434bool ARMAsmParser::
1435CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1436 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1437 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1438
1439 // Create a writeback register dummy placeholder.
1440 Inst.addOperand(MCOperand::CreateImm(0));
1441
1442 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1443 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1444 return true;
1445}
1446
1447/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1448/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1449/// when they refer multiple MIOperands inside a single one.
1450bool ARMAsmParser::
1451CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1452 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1453 // Create a writeback register dummy placeholder.
1454 Inst.addOperand(MCOperand::CreateImm(0));
1455 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1456 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1457 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1458 return true;
1459}
1460
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001461/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1462/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1463/// when they refer multiple MIOperands inside a single one.
1464bool ARMAsmParser::
1465CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1466 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1467 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1468
1469 // Create a writeback register dummy placeholder.
1470 Inst.addOperand(MCOperand::CreateImm(0));
1471
1472 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1473 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1474 return true;
1475}
1476
1477/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1478/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1479/// when they refer multiple MIOperands inside a single one.
1480bool ARMAsmParser::
1481CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1482 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1483 // Create a writeback register dummy placeholder.
1484 Inst.addOperand(MCOperand::CreateImm(0));
1485 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1486 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1487 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1488 return true;
1489}
1490
Bill Wendlinge7176102010-11-06 22:36:58 +00001491/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001492/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001493///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001494/// TODO Only preindexing and postindexing addressing are started, unindexed
1495/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001496bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001497ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1498 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001499 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001500 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001501 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001502 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001503 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001504
Sean Callanan18b83232010-01-19 21:44:56 +00001505 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001506 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1507 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001508 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001509 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001510 int BaseRegNum = TryParseRegister();
1511 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001512 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001513 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001514 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001515
Daniel Dunbar05710932011-01-18 05:34:17 +00001516 // The next token must either be a comma or a closing bracket.
1517 const AsmToken &Tok = Parser.getTok();
1518 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1519 return true;
1520
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001521 bool Preindexed = false;
1522 bool Postindexed = false;
1523 bool OffsetIsReg = false;
1524 bool Negative = false;
1525 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001526 ARMOperand *WBOp = 0;
1527 int OffsetRegNum = -1;
1528 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001529 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001530 const MCExpr *ShiftAmount = 0;
1531 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001532
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001533 // First look for preindexed address forms, that is after the "[Rn" we now
1534 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001535 if (Tok.is(AsmToken::Comma)) {
1536 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001537 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001538
Chris Lattner550276e2010-10-28 20:52:15 +00001539 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1540 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001541 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001542 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001543 if (RBracTok.isNot(AsmToken::RBrac)) {
1544 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001545 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001546 }
Sean Callanan76264762010-04-02 22:27:05 +00001547 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001548 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001549
Sean Callanan18b83232010-01-19 21:44:56 +00001550 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001551 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001552 // None of addrmode3 instruction uses "!"
1553 if (AddrMode == ARMII::AddrMode3)
1554 return true;
1555
Bill Wendling50d0f582010-11-18 23:43:05 +00001556 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1557 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001558 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001559 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001560 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1561 if (AddrMode == ARMII::AddrMode2)
1562 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001563 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001564 } else {
1565 // The "[Rn" we have so far was not followed by a comma.
1566
Jim Grosbach80eb2332010-10-29 17:41:25 +00001567 // If there's anything other than the right brace, this is a post indexing
1568 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001569 E = Tok.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 &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001573
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001574 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001575 Postindexed = true;
1576 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001577
Chris Lattner550276e2010-10-28 20:52:15 +00001578 if (NextTok.isNot(AsmToken::Comma)) {
1579 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001580 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001581 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001582
Sean Callananb9a25b72010-01-19 20:27:46 +00001583 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001584
Chris Lattner550276e2010-10-28 20:52:15 +00001585 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001586 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001587 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001588 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001589 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001590 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001591
1592 // Force Offset to exist if used.
1593 if (!OffsetIsReg) {
1594 if (!Offset)
1595 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001596 } else {
1597 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1598 Error(E, "shift amount not supported");
1599 return true;
1600 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001601 }
1602
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001603 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1604 Offset, OffsetRegNum, OffsetRegShifted,
1605 ShiftType, ShiftAmount, Preindexed,
1606 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001607 if (WBOp)
1608 Operands.push_back(WBOp);
1609
1610 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001611}
1612
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001613/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1614/// we will parse the following (were +/- means that a plus or minus is
1615/// optional):
1616/// +/-Rm
1617/// +/-Rm, shift
1618/// #offset
1619/// we return false on success or an error otherwise.
1620bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001621 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001622 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001623 const MCExpr *&ShiftAmount,
1624 const MCExpr *&Offset,
1625 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001626 int &OffsetRegNum,
1627 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001628 Negative = false;
1629 OffsetRegShifted = false;
1630 OffsetIsReg = false;
1631 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001632 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001633 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001634 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001635 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001636 else if (NextTok.is(AsmToken::Minus)) {
1637 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001638 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001639 }
1640 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001641 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001642 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001643 SMLoc CurLoc = OffsetRegTok.getLoc();
1644 OffsetRegNum = TryParseRegister();
1645 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001646 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001647 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001648 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001649 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001650
Bill Wendling12f40e92010-11-06 10:51:53 +00001651 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001652 if (OffsetRegNum != -1) {
1653 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001654 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001655 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001656 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001657
Sean Callanan18b83232010-01-19 21:44:56 +00001658 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001659 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001660 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001661 OffsetRegShifted = true;
1662 }
1663 }
1664 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1665 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001666 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001667 if (HashTok.isNot(AsmToken::Hash))
1668 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001669
Sean Callananb9a25b72010-01-19 20:27:46 +00001670 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001671
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001672 if (getParser().ParseExpression(Offset))
1673 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001674 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001675 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001676 return false;
1677}
1678
1679/// ParseShift as one of these two:
1680/// ( lsl | lsr | asr | ror ) , # shift_amount
1681/// rrx
1682/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001683bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1684 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001685 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001686 if (Tok.isNot(AsmToken::Identifier))
1687 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001688 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001689 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001690 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001691 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001692 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001693 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001694 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001695 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001696 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001697 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001698 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001699 else
1700 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001701 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001702
1703 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001704 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001705 return false;
1706
1707 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001708 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001709 if (HashTok.isNot(AsmToken::Hash))
1710 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001711 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001712
1713 if (getParser().ParseExpression(ShiftAmount))
1714 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001715
1716 return false;
1717}
1718
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001719/// Parse a arm instruction operand. For now this parses the operand regardless
1720/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001721bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001722 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001723 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001724
1725 // Check if the current operand has a custom associated parser, if so, try to
1726 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001727 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1728 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001729 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001730 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1731 // there was a match, but an error occurred, in which case, just return that
1732 // the operand parsing failed.
1733 if (ResTy == MatchOperand_ParseFail)
1734 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001735
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001736 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001737 default:
1738 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001739 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001740 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001741 if (!TryParseRegisterWithWriteBack(Operands))
1742 return false;
Owen Anderson00828302011-03-18 22:50:18 +00001743 if (!TryParseShiftRegister(Operands))
1744 return false;
1745
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001746
1747 // Fall though for the Identifier case that is not a register or a
1748 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001749 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1750 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001751 // This was not a register so parse other operands that start with an
1752 // identifier (like labels) as expressions and create them as immediates.
1753 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001754 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001755 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001756 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001757 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001758 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1759 return false;
1760 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001761 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001762 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001763 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001764 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001765 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001766 // #42 -> immediate.
1767 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001768 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001769 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001770 const MCExpr *ImmVal;
1771 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001772 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001773 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001774 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1775 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001776 case AsmToken::Colon: {
1777 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001778 // FIXME: Check it's an expression prefix,
1779 // e.g. (FOO - :lower16:BAR) isn't legal.
1780 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001781 if (ParsePrefix(RefKind))
1782 return true;
1783
Evan Cheng75972122011-01-13 07:58:56 +00001784 const MCExpr *SubExprVal;
1785 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001786 return true;
1787
Evan Cheng75972122011-01-13 07:58:56 +00001788 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1789 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001790 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001791 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001792 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001793 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001794 }
1795}
1796
Evan Cheng75972122011-01-13 07:58:56 +00001797// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1798// :lower16: and :upper16:.
1799bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1800 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001801
1802 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001803 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001804 Parser.Lex(); // Eat ':'
1805
1806 if (getLexer().isNot(AsmToken::Identifier)) {
1807 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1808 return true;
1809 }
1810
1811 StringRef IDVal = Parser.getTok().getIdentifier();
1812 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001813 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001814 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001815 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001816 } else {
1817 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1818 return true;
1819 }
1820 Parser.Lex();
1821
1822 if (getLexer().isNot(AsmToken::Colon)) {
1823 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1824 return true;
1825 }
1826 Parser.Lex(); // Eat the last ':'
1827 return false;
1828}
1829
1830const MCExpr *
1831ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1832 MCSymbolRefExpr::VariantKind Variant) {
1833 // Recurse over the given expression, rebuilding it to apply the given variant
1834 // to the leftmost symbol.
1835 if (Variant == MCSymbolRefExpr::VK_None)
1836 return E;
1837
1838 switch (E->getKind()) {
1839 case MCExpr::Target:
1840 llvm_unreachable("Can't handle target expr yet");
1841 case MCExpr::Constant:
1842 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1843
1844 case MCExpr::SymbolRef: {
1845 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1846
1847 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1848 return 0;
1849
1850 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1851 }
1852
1853 case MCExpr::Unary:
1854 llvm_unreachable("Can't handle unary expressions yet");
1855
1856 case MCExpr::Binary: {
1857 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1858 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1859 const MCExpr *RHS = BE->getRHS();
1860 if (!LHS)
1861 return 0;
1862
1863 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1864 }
1865 }
1866
1867 assert(0 && "Invalid expression kind!");
1868 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001869}
1870
Daniel Dunbar352e1482011-01-11 15:59:50 +00001871/// \brief Given a mnemonic, split out possible predication code and carry
1872/// setting letters to form a canonical mnemonic and flags.
1873//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001874// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001875static StringRef SplitMnemonic(StringRef Mnemonic,
1876 unsigned &PredicationCode,
1877 bool &CarrySetting,
1878 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001879 PredicationCode = ARMCC::AL;
1880 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001881 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001882
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001883 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001884 //
1885 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001886 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1887 Mnemonic == "movs" ||
1888 Mnemonic == "svc" ||
1889 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1890 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1891 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1892 Mnemonic == "vclt" ||
1893 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1894 Mnemonic == "vcle" ||
1895 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1896 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbachd1f0bbe2011-06-27 20:59:10 +00001897 Mnemonic == "vqdmlal" || Mnemonic == "bics"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001898 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001899
Jim Grosbach3f00e312011-07-11 17:09:57 +00001900 // First, split out any predication code. Ignore mnemonics we know aren't
1901 // predicated but do have a carry-set and so weren't caught above.
1902 if (Mnemonic != "adcs") {
1903 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1904 .Case("eq", ARMCC::EQ)
1905 .Case("ne", ARMCC::NE)
1906 .Case("hs", ARMCC::HS)
1907 .Case("cs", ARMCC::HS)
1908 .Case("lo", ARMCC::LO)
1909 .Case("cc", ARMCC::LO)
1910 .Case("mi", ARMCC::MI)
1911 .Case("pl", ARMCC::PL)
1912 .Case("vs", ARMCC::VS)
1913 .Case("vc", ARMCC::VC)
1914 .Case("hi", ARMCC::HI)
1915 .Case("ls", ARMCC::LS)
1916 .Case("ge", ARMCC::GE)
1917 .Case("lt", ARMCC::LT)
1918 .Case("gt", ARMCC::GT)
1919 .Case("le", ARMCC::LE)
1920 .Case("al", ARMCC::AL)
1921 .Default(~0U);
1922 if (CC != ~0U) {
1923 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
1924 PredicationCode = CC;
1925 }
Bill Wendling52925b62010-10-29 23:50:21 +00001926 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001927
Daniel Dunbar352e1482011-01-11 15:59:50 +00001928 // Next, determine if we have a carry setting bit. We explicitly ignore all
1929 // the instructions we know end in 's'.
1930 if (Mnemonic.endswith("s") &&
1931 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1932 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1933 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1934 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1935 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1936 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1937 CarrySetting = true;
1938 }
1939
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001940 // The "cps" instruction can have a interrupt mode operand which is glued into
1941 // the mnemonic. Check if this is the case, split it and parse the imod op
1942 if (Mnemonic.startswith("cps")) {
1943 // Split out any imod code.
1944 unsigned IMod =
1945 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1946 .Case("ie", ARM_PROC::IE)
1947 .Case("id", ARM_PROC::ID)
1948 .Default(~0U);
1949 if (IMod != ~0U) {
1950 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1951 ProcessorIMod = IMod;
1952 }
1953 }
1954
Daniel Dunbar352e1482011-01-11 15:59:50 +00001955 return Mnemonic;
1956}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001957
1958/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1959/// inclusion of carry set or predication code operands.
1960//
1961// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001962void ARMAsmParser::
1963GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1964 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001965 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1966 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1967 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1968 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001969 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001970 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1971 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001972 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00001973 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001974 CanAcceptCarrySet = true;
1975 } else {
1976 CanAcceptCarrySet = false;
1977 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001978
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001979 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1980 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1981 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1982 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001983 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001984 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001985 CanAcceptPredicationCode = false;
1986 } else {
1987 CanAcceptPredicationCode = true;
1988 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001989
Evan Chengebdeeab2011-07-08 01:53:10 +00001990 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001991 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001992 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001993 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001994}
1995
1996/// Parse an arm instruction mnemonic followed by its operands.
1997bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1998 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1999 // Create the leading tokens for the mnemonic, split by '.' characters.
2000 size_t Start = 0, Next = Name.find('.');
2001 StringRef Head = Name.slice(Start, Next);
2002
Daniel Dunbar352e1482011-01-11 15:59:50 +00002003 // Split out the predication code and carry setting flag from the mnemonic.
2004 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002005 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002006 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002007 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
2008 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002009
Chris Lattner3a697562010-10-28 17:20:03 +00002010 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00002011
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002012 // Next, add the CCOut and ConditionCode operands, if needed.
2013 //
2014 // For mnemonics which can ever incorporate a carry setting bit or predication
2015 // code, our matching model involves us always generating CCOut and
2016 // ConditionCode operands to match the mnemonic "as written" and then we let
2017 // the matcher deal with finding the right instruction or generating an
2018 // appropriate error.
2019 bool CanAcceptCarrySet, CanAcceptPredicationCode;
2020 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
2021
2022 // Add the carry setting operand, if necessary.
2023 //
2024 // FIXME: It would be awesome if we could somehow invent a location such that
2025 // match errors on this operand would print a nice diagnostic about how the
2026 // 's' character in the mnemonic resulted in a CCOut operand.
2027 if (CanAcceptCarrySet) {
2028 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2029 NameLoc));
2030 } else {
2031 // This mnemonic can't ever accept a carry set, but the user wrote one (or
2032 // misspelled another mnemonic).
2033
2034 // FIXME: Issue a nice error.
2035 }
2036
2037 // Add the predication code operand, if necessary.
2038 if (CanAcceptPredicationCode) {
2039 Operands.push_back(ARMOperand::CreateCondCode(
2040 ARMCC::CondCodes(PredicationCode), NameLoc));
2041 } else {
2042 // This mnemonic can't ever accept a predication code, but the user wrote
2043 // one (or misspelled another mnemonic).
2044
2045 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002046 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002047
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002048 // Add the processor imod operand, if necessary.
2049 if (ProcessorIMod) {
2050 Operands.push_back(ARMOperand::CreateImm(
2051 MCConstantExpr::Create(ProcessorIMod, getContext()),
2052 NameLoc, NameLoc));
2053 } else {
2054 // This mnemonic can't ever accept a imod, but the user wrote
2055 // one (or misspelled another mnemonic).
2056
2057 // FIXME: Issue a nice error.
2058 }
2059
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002060 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002061 while (Next != StringRef::npos) {
2062 Start = Next;
2063 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002064 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002065
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002066 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002067 }
2068
2069 // Read the remaining operands.
2070 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002071 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002072 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002073 Parser.EatToEndOfStatement();
2074 return true;
2075 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002076
2077 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002078 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002079
2080 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002081 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002082 Parser.EatToEndOfStatement();
2083 return true;
2084 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002085 }
2086 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002087
Chris Lattnercbf8a982010-09-11 16:18:25 +00002088 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2089 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002090 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002091 }
Bill Wendling146018f2010-11-06 21:42:12 +00002092
Chris Lattner34e53142010-09-08 05:10:46 +00002093 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00002094 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002095}
2096
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002097bool ARMAsmParser::
2098MatchAndEmitInstruction(SMLoc IDLoc,
2099 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2100 MCStreamer &Out) {
2101 MCInst Inst;
2102 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002103 MatchResultTy MatchResult, MatchResult2;
2104 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2105 if (MatchResult != Match_Success) {
2106 // If we get a Match_InvalidOperand it might be some arithmetic instruction
2107 // that does not update the condition codes. So try adding a CCOut operand
2108 // with a value of reg0.
2109 if (MatchResult == Match_InvalidOperand) {
2110 Operands.insert(Operands.begin() + 1,
2111 ARMOperand::CreateCCOut(0,
2112 ((ARMOperand*)Operands[0])->getStartLoc()));
2113 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2114 if (MatchResult2 == Match_Success)
2115 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002116 else {
2117 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002118 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002119 delete CCOut;
2120 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002121 }
2122 // If we get a Match_MnemonicFail it might be some arithmetic instruction
2123 // that updates the condition codes if it ends in 's'. So see if the
2124 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
2125 // operand with a value of CPSR.
Evan Chengeb0caa12011-07-08 22:49:55 +00002126 else if (MatchResult == Match_MnemonicFail) {
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002127 // Get the instruction mnemonic, which is the first token.
2128 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
2129 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
2130 // removed the 's' from the mnemonic for matching.
2131 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
2132 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002133 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2134 Operands.erase(Operands.begin());
2135 delete OldMnemonic;
2136 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002137 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
2138 Operands.insert(Operands.begin() + 1,
2139 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2140 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2141 if (MatchResult2 == Match_Success)
2142 MatchResult = Match_Success;
2143 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002144 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2145 Operands.erase(Operands.begin());
2146 delete OldMnemonic;
2147 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002148 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002149 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2150 Operands.erase(Operands.begin() + 1);
2151 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002152 }
2153 }
2154 }
2155 }
2156 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002157 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002158 Out.EmitInstruction(Inst);
2159 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002160 case Match_MissingFeature:
2161 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2162 return true;
2163 case Match_InvalidOperand: {
2164 SMLoc ErrorLoc = IDLoc;
2165 if (ErrorInfo != ~0U) {
2166 if (ErrorInfo >= Operands.size())
2167 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002168
Chris Lattnere73d4f82010-10-28 21:41:58 +00002169 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2170 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2171 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002172
Chris Lattnere73d4f82010-10-28 21:41:58 +00002173 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002174 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002175 case Match_MnemonicFail:
2176 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002177 case Match_ConversionFail:
2178 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002179 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002180
Eric Christopherc223e2b2010-10-29 09:26:59 +00002181 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002182 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002183}
2184
Kevin Enderby515d5092009-10-15 20:48:48 +00002185/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002186bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2187 StringRef IDVal = DirectiveID.getIdentifier();
2188 if (IDVal == ".word")
2189 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002190 else if (IDVal == ".thumb")
2191 return ParseDirectiveThumb(DirectiveID.getLoc());
2192 else if (IDVal == ".thumb_func")
2193 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2194 else if (IDVal == ".code")
2195 return ParseDirectiveCode(DirectiveID.getLoc());
2196 else if (IDVal == ".syntax")
2197 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002198 return true;
2199}
2200
2201/// ParseDirectiveWord
2202/// ::= .word [ expression (, expression)* ]
2203bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2204 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2205 for (;;) {
2206 const MCExpr *Value;
2207 if (getParser().ParseExpression(Value))
2208 return true;
2209
Chris Lattneraaec2052010-01-19 19:46:13 +00002210 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002211
2212 if (getLexer().is(AsmToken::EndOfStatement))
2213 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002214
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002215 // FIXME: Improve diagnostic.
2216 if (getLexer().isNot(AsmToken::Comma))
2217 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002218 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002219 }
2220 }
2221
Sean Callananb9a25b72010-01-19 20:27:46 +00002222 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002223 return false;
2224}
2225
Kevin Enderby515d5092009-10-15 20:48:48 +00002226/// ParseDirectiveThumb
2227/// ::= .thumb
2228bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2229 if (getLexer().isNot(AsmToken::EndOfStatement))
2230 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002231 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002232
2233 // TODO: set thumb mode
2234 // TODO: tell the MC streamer the mode
2235 // getParser().getStreamer().Emit???();
2236 return false;
2237}
2238
2239/// ParseDirectiveThumbFunc
2240/// ::= .thumbfunc symbol_name
2241bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002242 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2243 bool isMachO = MAI.hasSubsectionsViaSymbols();
2244 StringRef Name;
2245
2246 // Darwin asm has function name after .thumb_func direction
2247 // ELF doesn't
2248 if (isMachO) {
2249 const AsmToken &Tok = Parser.getTok();
2250 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2251 return Error(L, "unexpected token in .thumb_func directive");
2252 Name = Tok.getString();
2253 Parser.Lex(); // Consume the identifier token.
2254 }
2255
Kevin Enderby515d5092009-10-15 20:48:48 +00002256 if (getLexer().isNot(AsmToken::EndOfStatement))
2257 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002258 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002259
Rafael Espindola64695402011-05-16 16:17:21 +00002260 // FIXME: assuming function name will be the line following .thumb_func
2261 if (!isMachO) {
2262 Name = Parser.getTok().getString();
2263 }
2264
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002265 // Mark symbol as a thumb symbol.
2266 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2267 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002268 return false;
2269}
2270
2271/// ParseDirectiveSyntax
2272/// ::= .syntax unified | divided
2273bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002274 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002275 if (Tok.isNot(AsmToken::Identifier))
2276 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002277 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002278 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002279 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002280 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002281 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002282 else
2283 return Error(L, "unrecognized syntax mode in .syntax directive");
2284
2285 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002286 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002287 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002288
2289 // TODO tell the MC streamer the mode
2290 // getParser().getStreamer().Emit???();
2291 return false;
2292}
2293
2294/// ParseDirectiveCode
2295/// ::= .code 16 | 32
2296bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002297 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002298 if (Tok.isNot(AsmToken::Integer))
2299 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002300 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002301 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002302 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002303 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002304 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002305 else
2306 return Error(L, "invalid operand to .code directive");
2307
2308 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002309 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002310 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002311
Evan Cheng32869202011-07-08 22:36:29 +00002312 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002313 if (!isThumb())
2314 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002315 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002316 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002317 if (isThumb())
2318 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002319 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002320 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002321
Kevin Enderby515d5092009-10-15 20:48:48 +00002322 return false;
2323}
2324
Sean Callanan90b70972010-04-07 20:29:34 +00002325extern "C" void LLVMInitializeARMAsmLexer();
2326
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002327/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002328extern "C" void LLVMInitializeARMAsmParser() {
2329 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2330 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002331 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002332}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002333
Chris Lattner0692ee62010-09-06 19:11:01 +00002334#define GET_REGISTER_MATCHER
2335#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002336#include "ARMGenAsmMatcher.inc"