blob: 8bfefdfb3a3cfd88b6e428786894f9d2751b8b7e [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,
Owen Anderson00828302011-03-18 22:50:18 +0000167 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000168 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000169 } Kind;
170
Sean Callanan76264762010-04-02 22:27:05 +0000171 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000172 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000173
174 union {
175 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000176 ARMCC::CondCodes Val;
177 } CC;
178
179 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000180 ARM_MB::MemBOpt Val;
181 } MBOpt;
182
183 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000184 unsigned Val;
185 } Cop;
186
187 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000188 ARM_PROC::IFlags Val;
189 } IFlags;
190
191 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000192 unsigned Val;
193 } MMask;
194
195 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000196 const char *Data;
197 unsigned Length;
198 } Tok;
199
200 struct {
201 unsigned RegNum;
202 } Reg;
203
Bill Wendling8155e5b2010-11-06 22:19:43 +0000204 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000205 const MCExpr *Val;
206 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000207
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000208 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000209 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000210 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000211 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000212 union {
213 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
214 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
215 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000216 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000217 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000218 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000219 unsigned Preindexed : 1;
220 unsigned Postindexed : 1;
221 unsigned OffsetIsReg : 1;
222 unsigned Negative : 1; // only used when OffsetIsReg is true
223 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000224 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000225
226 struct {
227 ARM_AM::ShiftOpc ShiftTy;
228 unsigned RegNum;
229 } Shift;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000230 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000231
Bill Wendling146018f2010-11-06 21:42:12 +0000232 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
233public:
Sean Callanan76264762010-04-02 22:27:05 +0000234 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
235 Kind = o.Kind;
236 StartLoc = o.StartLoc;
237 EndLoc = o.EndLoc;
238 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000239 case CondCode:
240 CC = o.CC;
241 break;
Sean Callanan76264762010-04-02 22:27:05 +0000242 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000243 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000244 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000245 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000246 case Register:
247 Reg = o.Reg;
248 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000249 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000250 case DPRRegisterList:
251 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000252 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000253 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000254 case CoprocNum:
255 case CoprocReg:
256 Cop = o.Cop;
257 break;
Sean Callanan76264762010-04-02 22:27:05 +0000258 case Immediate:
259 Imm = o.Imm;
260 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000261 case MemBarrierOpt:
262 MBOpt = o.MBOpt;
263 break;
Sean Callanan76264762010-04-02 22:27:05 +0000264 case Memory:
265 Mem = o.Mem;
266 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000267 case MSRMask:
268 MMask = o.MMask;
269 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000270 case ProcIFlags:
271 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000272 break;
273 case Shifter:
274 Shift = o.Shift;
275 break;
Sean Callanan76264762010-04-02 22:27:05 +0000276 }
277 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000278
Sean Callanan76264762010-04-02 22:27:05 +0000279 /// getStartLoc - Get the location of the first token of this operand.
280 SMLoc getStartLoc() const { return StartLoc; }
281 /// getEndLoc - Get the location of the last token of this operand.
282 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000283
Daniel Dunbar8462b302010-08-11 06:36:53 +0000284 ARMCC::CondCodes getCondCode() const {
285 assert(Kind == CondCode && "Invalid access!");
286 return CC.Val;
287 }
288
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000289 unsigned getCoproc() const {
290 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
291 return Cop.Val;
292 }
293
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000294 StringRef getToken() const {
295 assert(Kind == Token && "Invalid access!");
296 return StringRef(Tok.Data, Tok.Length);
297 }
298
299 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000300 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000301 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000302 }
303
Bill Wendling5fa22a12010-11-09 23:28:44 +0000304 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000305 assert((Kind == RegisterList || Kind == DPRRegisterList ||
306 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000307 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000308 }
309
Kevin Enderbycfe07242009-10-13 22:19:02 +0000310 const MCExpr *getImm() const {
311 assert(Kind == Immediate && "Invalid access!");
312 return Imm.Val;
313 }
314
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000315 ARM_MB::MemBOpt getMemBarrierOpt() const {
316 assert(Kind == MemBarrierOpt && "Invalid access!");
317 return MBOpt.Val;
318 }
319
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000320 ARM_PROC::IFlags getProcIFlags() const {
321 assert(Kind == ProcIFlags && "Invalid access!");
322 return IFlags.Val;
323 }
324
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000325 unsigned getMSRMask() const {
326 assert(Kind == MSRMask && "Invalid access!");
327 return MMask.Val;
328 }
329
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000330 /// @name Memory Operand Accessors
331 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000332 ARMII::AddrMode getMemAddrMode() const {
333 return Mem.AddrMode;
334 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000335 unsigned getMemBaseRegNum() const {
336 return Mem.BaseRegNum;
337 }
338 unsigned getMemOffsetRegNum() const {
339 assert(Mem.OffsetIsReg && "Invalid access!");
340 return Mem.Offset.RegNum;
341 }
342 const MCExpr *getMemOffset() const {
343 assert(!Mem.OffsetIsReg && "Invalid access!");
344 return Mem.Offset.Value;
345 }
346 unsigned getMemOffsetRegShifted() const {
347 assert(Mem.OffsetIsReg && "Invalid access!");
348 return Mem.OffsetRegShifted;
349 }
350 const MCExpr *getMemShiftAmount() const {
351 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
352 return Mem.ShiftAmount;
353 }
Owen Anderson00828302011-03-18 22:50:18 +0000354 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000355 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
356 return Mem.ShiftType;
357 }
358 bool getMemPreindexed() const { return Mem.Preindexed; }
359 bool getMemPostindexed() const { return Mem.Postindexed; }
360 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
361 bool getMemNegative() const { return Mem.Negative; }
362 bool getMemWriteback() const { return Mem.Writeback; }
363
364 /// @}
365
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000366 bool isCoprocNum() const { return Kind == CoprocNum; }
367 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000368 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000369 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000370 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000371 bool isImm0_255() const {
372 if (Kind != Immediate)
373 return false;
374 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
375 if (!CE) return false;
376 int64_t Value = CE->getValue();
377 return Value >= 0 && Value < 256;
378 }
379 bool isT2SOImm() const {
380 if (Kind != Immediate)
381 return false;
382 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
383 if (!CE) return false;
384 int64_t Value = CE->getValue();
385 return ARM_AM::getT2SOImmVal(Value) != -1;
386 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000387 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000388 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000389 bool isDPRRegList() const { return Kind == DPRRegisterList; }
390 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000391 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000392 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000393 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000394 bool isShifter() const { return Kind == Shifter; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000395 bool isMemMode2() const {
396 if (getMemAddrMode() != ARMII::AddrMode2)
397 return false;
398
399 if (getMemOffsetIsReg())
400 return true;
401
402 if (getMemNegative() &&
403 !(getMemPostindexed() || getMemPreindexed()))
404 return false;
405
406 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
407 if (!CE) return false;
408 int64_t Value = CE->getValue();
409
410 // The offset must be in the range 0-4095 (imm12).
411 if (Value > 4095 || Value < -4095)
412 return false;
413
414 return true;
415 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000416 bool isMemMode3() const {
417 if (getMemAddrMode() != ARMII::AddrMode3)
418 return false;
419
420 if (getMemOffsetIsReg()) {
421 if (getMemOffsetRegShifted())
422 return false; // No shift with offset reg allowed
423 return true;
424 }
425
426 if (getMemNegative() &&
427 !(getMemPostindexed() || getMemPreindexed()))
428 return false;
429
430 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
431 if (!CE) return false;
432 int64_t Value = CE->getValue();
433
434 // The offset must be in the range 0-255 (imm8).
435 if (Value > 255 || Value < -255)
436 return false;
437
438 return true;
439 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000440 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000441 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
442 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000443 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000444
Daniel Dunbar4b462672011-01-18 05:55:27 +0000445 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000446 if (!CE) return false;
447
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000448 // The offset must be a multiple of 4 in the range 0-1020.
449 int64_t Value = CE->getValue();
450 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
451 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000452 bool isMemMode7() const {
453 if (!isMemory() ||
454 getMemPreindexed() ||
455 getMemPostindexed() ||
456 getMemOffsetIsReg() ||
457 getMemNegative() ||
458 getMemWriteback())
459 return false;
460
461 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
462 if (!CE) return false;
463
464 if (CE->getValue())
465 return false;
466
467 return true;
468 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000469 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000470 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000471 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000472 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000473 }
474 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000475 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000476 return false;
477
Daniel Dunbar4b462672011-01-18 05:55:27 +0000478 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000479 if (!CE) return false;
480
481 // The offset must be a multiple of 4 in the range 0-124.
482 uint64_t Value = CE->getValue();
483 return ((Value & 0x3) == 0 && Value <= 124);
484 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000485 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000486 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000487
488 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000489 // Add as immediates when possible. Null MCExpr = 0.
490 if (Expr == 0)
491 Inst.addOperand(MCOperand::CreateImm(0));
492 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000493 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
494 else
495 Inst.addOperand(MCOperand::CreateExpr(Expr));
496 }
497
Daniel Dunbar8462b302010-08-11 06:36:53 +0000498 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000499 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000500 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000501 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
502 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000503 }
504
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000505 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
506 assert(N == 1 && "Invalid number of operands!");
507 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
508 }
509
510 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
511 assert(N == 1 && "Invalid number of operands!");
512 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
513 }
514
Jim Grosbachd67641b2010-12-06 18:21:12 +0000515 void addCCOutOperands(MCInst &Inst, unsigned N) const {
516 assert(N == 1 && "Invalid number of operands!");
517 Inst.addOperand(MCOperand::CreateReg(getReg()));
518 }
519
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000520 void addRegOperands(MCInst &Inst, unsigned N) const {
521 assert(N == 1 && "Invalid number of operands!");
522 Inst.addOperand(MCOperand::CreateReg(getReg()));
523 }
524
Owen Anderson00828302011-03-18 22:50:18 +0000525 void addShifterOperands(MCInst &Inst, unsigned N) const {
526 assert(N == 1 && "Invalid number of operands!");
527 Inst.addOperand(MCOperand::CreateImm(
528 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
529 }
530
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000531 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000532 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000533 const SmallVectorImpl<unsigned> &RegList = getRegList();
534 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000535 I = RegList.begin(), E = RegList.end(); I != E; ++I)
536 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000537 }
538
Bill Wendling0f630752010-11-17 04:32:08 +0000539 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
540 addRegListOperands(Inst, N);
541 }
542
543 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
544 addRegListOperands(Inst, N);
545 }
546
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000547 void addImmOperands(MCInst &Inst, unsigned N) const {
548 assert(N == 1 && "Invalid number of operands!");
549 addExpr(Inst, getImm());
550 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000551
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000552 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
553 assert(N == 1 && "Invalid number of operands!");
554 addExpr(Inst, getImm());
555 }
556
557 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
558 assert(N == 1 && "Invalid number of operands!");
559 addExpr(Inst, getImm());
560 }
561
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000562 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
563 assert(N == 1 && "Invalid number of operands!");
564 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
565 }
566
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000567 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
568 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
569 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
570
571 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000572 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000573 assert((CE || CE->getValue() == 0) &&
574 "No offset operand support in mode 7");
575 }
576
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000577 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
578 assert(isMemMode2() && "Invalid mode or number of operands!");
579 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
580 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
581
582 if (getMemOffsetIsReg()) {
583 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
584
585 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
586 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
587 int64_t ShiftAmount = 0;
588
589 if (getMemOffsetRegShifted()) {
590 ShOpc = getMemShiftType();
591 const MCConstantExpr *CE =
592 dyn_cast<MCConstantExpr>(getMemShiftAmount());
593 ShiftAmount = CE->getValue();
594 }
595
596 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
597 ShOpc, IdxMode)));
598 return;
599 }
600
601 // Create a operand placeholder to always yield the same number of operands.
602 Inst.addOperand(MCOperand::CreateReg(0));
603
604 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
605 // the difference?
606 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
607 assert(CE && "Non-constant mode 2 offset operand!");
608 int64_t Offset = CE->getValue();
609
610 if (Offset >= 0)
611 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
612 Offset, ARM_AM::no_shift, IdxMode)));
613 else
614 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
615 -Offset, ARM_AM::no_shift, IdxMode)));
616 }
617
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000618 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
619 assert(isMemMode3() && "Invalid mode or number of operands!");
620 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
621 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
622
623 if (getMemOffsetIsReg()) {
624 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
625
626 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
627 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
628 IdxMode)));
629 return;
630 }
631
632 // Create a operand placeholder to always yield the same number of operands.
633 Inst.addOperand(MCOperand::CreateReg(0));
634
635 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
636 // the difference?
637 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
638 assert(CE && "Non-constant mode 3 offset operand!");
639 int64_t Offset = CE->getValue();
640
641 if (Offset >= 0)
642 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
643 Offset, IdxMode)));
644 else
645 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
646 -Offset, IdxMode)));
647 }
648
Chris Lattner14b93852010-10-29 00:27:31 +0000649 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
650 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000651
Daniel Dunbar4b462672011-01-18 05:55:27 +0000652 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
653 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000654
Jim Grosbach80eb2332010-10-29 17:41:25 +0000655 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
656 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000657 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000658 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000659
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000660 // The MCInst offset operand doesn't include the low two bits (like
661 // the instruction encoding).
662 int64_t Offset = CE->getValue() / 4;
663 if (Offset >= 0)
664 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
665 Offset)));
666 else
667 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
668 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000669 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000670
Bill Wendlingf4caf692010-12-14 03:36:38 +0000671 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
672 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000673 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
674 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000675 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000676
Bill Wendlingf4caf692010-12-14 03:36:38 +0000677 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
678 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000679 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
680 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000681 assert(CE && "Non-constant mode offset operand!");
682 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000683 }
684
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000685 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
686 assert(N == 1 && "Invalid number of operands!");
687 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
688 }
689
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000690 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
691 assert(N == 1 && "Invalid number of operands!");
692 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
693 }
694
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000695 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000696
Chris Lattner3a697562010-10-28 17:20:03 +0000697 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
698 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000699 Op->CC.Val = CC;
700 Op->StartLoc = S;
701 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000702 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000703 }
704
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000705 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
706 ARMOperand *Op = new ARMOperand(CoprocNum);
707 Op->Cop.Val = CopVal;
708 Op->StartLoc = S;
709 Op->EndLoc = S;
710 return Op;
711 }
712
713 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
714 ARMOperand *Op = new ARMOperand(CoprocReg);
715 Op->Cop.Val = CopVal;
716 Op->StartLoc = S;
717 Op->EndLoc = S;
718 return Op;
719 }
720
Jim Grosbachd67641b2010-12-06 18:21:12 +0000721 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
722 ARMOperand *Op = new ARMOperand(CCOut);
723 Op->Reg.RegNum = RegNum;
724 Op->StartLoc = S;
725 Op->EndLoc = S;
726 return Op;
727 }
728
Chris Lattner3a697562010-10-28 17:20:03 +0000729 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
730 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000731 Op->Tok.Data = Str.data();
732 Op->Tok.Length = Str.size();
733 Op->StartLoc = S;
734 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000735 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000736 }
737
Bill Wendling50d0f582010-11-18 23:43:05 +0000738 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000739 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000740 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000741 Op->StartLoc = S;
742 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000743 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000744 }
745
Owen Anderson00828302011-03-18 22:50:18 +0000746 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
747 SMLoc S, SMLoc E) {
748 ARMOperand *Op = new ARMOperand(Shifter);
749 Op->Shift.ShiftTy = ShTy;
750 Op->StartLoc = S;
751 Op->EndLoc = E;
752 return Op;
753 }
754
Bill Wendling7729e062010-11-09 22:44:22 +0000755 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000756 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000757 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000758 KindTy Kind = RegisterList;
759
760 if (ARM::DPRRegClass.contains(Regs.front().first))
761 Kind = DPRRegisterList;
762 else if (ARM::SPRRegClass.contains(Regs.front().first))
763 Kind = SPRRegisterList;
764
765 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000766 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000767 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000768 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000769 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000770 Op->StartLoc = StartLoc;
771 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000772 return Op;
773 }
774
Chris Lattner3a697562010-10-28 17:20:03 +0000775 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
776 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000777 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000778 Op->StartLoc = S;
779 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000780 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000781 }
782
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000783 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
784 bool OffsetIsReg, const MCExpr *Offset,
785 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000786 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000787 const MCExpr *ShiftAmount, bool Preindexed,
788 bool Postindexed, bool Negative, bool Writeback,
789 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000790 assert((OffsetRegNum == -1 || OffsetIsReg) &&
791 "OffsetRegNum must imply OffsetIsReg!");
792 assert((!OffsetRegShifted || OffsetIsReg) &&
793 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000794 assert((Offset || OffsetIsReg) &&
795 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000796 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
797 "Cannot have shift amount without shifted register offset!");
798 assert((!Offset || !OffsetIsReg) &&
799 "Cannot have expression offset and register offset!");
800
Chris Lattner3a697562010-10-28 17:20:03 +0000801 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000802 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000803 Op->Mem.BaseRegNum = BaseRegNum;
804 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000805 if (OffsetIsReg)
806 Op->Mem.Offset.RegNum = OffsetRegNum;
807 else
808 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000809 Op->Mem.OffsetRegShifted = OffsetRegShifted;
810 Op->Mem.ShiftType = ShiftType;
811 Op->Mem.ShiftAmount = ShiftAmount;
812 Op->Mem.Preindexed = Preindexed;
813 Op->Mem.Postindexed = Postindexed;
814 Op->Mem.Negative = Negative;
815 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000816
Sean Callanan76264762010-04-02 22:27:05 +0000817 Op->StartLoc = S;
818 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000819 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000820 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000821
822 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
823 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
824 Op->MBOpt.Val = Opt;
825 Op->StartLoc = S;
826 Op->EndLoc = S;
827 return Op;
828 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000829
830 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
831 ARMOperand *Op = new ARMOperand(ProcIFlags);
832 Op->IFlags.Val = IFlags;
833 Op->StartLoc = S;
834 Op->EndLoc = S;
835 return Op;
836 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000837
838 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
839 ARMOperand *Op = new ARMOperand(MSRMask);
840 Op->MMask.Val = MMask;
841 Op->StartLoc = S;
842 Op->EndLoc = S;
843 return Op;
844 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000845};
846
847} // end anonymous namespace.
848
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000849void ARMOperand::dump(raw_ostream &OS) const {
850 switch (Kind) {
851 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000852 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000853 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000854 case CCOut:
855 OS << "<ccout " << getReg() << ">";
856 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000857 case CoprocNum:
858 OS << "<coprocessor number: " << getCoproc() << ">";
859 break;
860 case CoprocReg:
861 OS << "<coprocessor register: " << getCoproc() << ">";
862 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000863 case MSRMask:
864 OS << "<mask: " << getMSRMask() << ">";
865 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000866 case Immediate:
867 getImm()->print(OS);
868 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000869 case MemBarrierOpt:
870 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
871 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000872 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000873 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000874 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
875 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000876 if (getMemOffsetIsReg()) {
877 OS << " offset:<register " << getMemOffsetRegNum();
878 if (getMemOffsetRegShifted()) {
879 OS << " offset-shift-type:" << getMemShiftType();
880 OS << " offset-shift-amount:" << *getMemShiftAmount();
881 }
882 } else {
883 OS << " offset:" << *getMemOffset();
884 }
885 if (getMemOffsetIsReg())
886 OS << " (offset-is-reg)";
887 if (getMemPreindexed())
888 OS << " (pre-indexed)";
889 if (getMemPostindexed())
890 OS << " (post-indexed)";
891 if (getMemNegative())
892 OS << " (negative)";
893 if (getMemWriteback())
894 OS << " (writeback)";
895 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000896 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000897 case ProcIFlags: {
898 OS << "<ARM_PROC::";
899 unsigned IFlags = getProcIFlags();
900 for (int i=2; i >= 0; --i)
901 if (IFlags & (1 << i))
902 OS << ARM_PROC::IFlagsToString(1 << i);
903 OS << ">";
904 break;
905 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000906 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000907 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000908 break;
Owen Anderson00828302011-03-18 22:50:18 +0000909 case Shifter:
910 OS << "<shifter " << getShiftOpcStr(Shift.ShiftTy) << ">";
911 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000912 case RegisterList:
913 case DPRRegisterList:
914 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000915 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000916
Bill Wendling5fa22a12010-11-09 23:28:44 +0000917 const SmallVectorImpl<unsigned> &RegList = getRegList();
918 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000919 I = RegList.begin(), E = RegList.end(); I != E; ) {
920 OS << *I;
921 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000922 }
923
924 OS << ">";
925 break;
926 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000927 case Token:
928 OS << "'" << getToken() << "'";
929 break;
930 }
931}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000932
933/// @name Auto-generated Match Functions
934/// {
935
936static unsigned MatchRegisterName(StringRef Name);
937
938/// }
939
Bob Wilson69df7232011-02-03 21:46:10 +0000940bool ARMAsmParser::ParseRegister(unsigned &RegNo,
941 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000942 RegNo = TryParseRegister();
943
944 return (RegNo == (unsigned)-1);
945}
946
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000947/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000948/// and if it is a register name the token is eaten and the register number is
949/// returned. Otherwise return -1.
950///
951int ARMAsmParser::TryParseRegister() {
952 const AsmToken &Tok = Parser.getTok();
953 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000954
Chris Lattnere5658fa2010-10-30 04:09:10 +0000955 // FIXME: Validate register for the current architecture; we have to do
956 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000957 std::string upperCase = Tok.getString().str();
958 std::string lowerCase = LowercaseString(upperCase);
959 unsigned RegNum = MatchRegisterName(lowerCase);
960 if (!RegNum) {
961 RegNum = StringSwitch<unsigned>(lowerCase)
962 .Case("r13", ARM::SP)
963 .Case("r14", ARM::LR)
964 .Case("r15", ARM::PC)
965 .Case("ip", ARM::R12)
966 .Default(0);
967 }
968 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000969
Chris Lattnere5658fa2010-10-30 04:09:10 +0000970 Parser.Lex(); // Eat identifier token.
971 return RegNum;
972}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000973
Owen Anderson00828302011-03-18 22:50:18 +0000974/// Try to parse a register name. The token must be an Identifier when called,
975/// and if it is a register name the token is eaten and the register number is
976/// returned. Otherwise return -1.
977///
978bool ARMAsmParser::TryParseShiftRegister(
979 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
980 SMLoc S = Parser.getTok().getLoc();
981 const AsmToken &Tok = Parser.getTok();
982 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
983
984 std::string upperCase = Tok.getString().str();
985 std::string lowerCase = LowercaseString(upperCase);
986 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
987 .Case("lsl", ARM_AM::lsl)
988 .Case("lsr", ARM_AM::lsr)
989 .Case("asr", ARM_AM::asr)
990 .Case("ror", ARM_AM::ror)
991 .Case("rrx", ARM_AM::rrx)
992 .Default(ARM_AM::no_shift);
993
994 if (ShiftTy == ARM_AM::no_shift)
995 return true;
996
997 Parser.Lex(); // Eat shift-type operand;
998 int RegNum = TryParseRegister();
999 if (RegNum == -1)
1000 return Error(Parser.getTok().getLoc(), "register expected");
1001
1002 Operands.push_back(ARMOperand::CreateReg(RegNum,S, Parser.getTok().getLoc()));
1003 Operands.push_back(ARMOperand::CreateShifter(ShiftTy,
1004 S, Parser.getTok().getLoc()));
1005
1006 return false;
1007}
1008
1009
Bill Wendling50d0f582010-11-18 23:43:05 +00001010/// Try to parse a register name. The token must be an Identifier when called.
1011/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1012/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001013///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001014/// TODO this is likely to change to allow different register types and or to
1015/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001016bool ARMAsmParser::
1017TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001018 SMLoc S = Parser.getTok().getLoc();
1019 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001020 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001021 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001022
Bill Wendling50d0f582010-11-18 23:43:05 +00001023 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001024
Chris Lattnere5658fa2010-10-30 04:09:10 +00001025 const AsmToken &ExclaimTok = Parser.getTok();
1026 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001027 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1028 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001029 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001030 }
1031
Bill Wendling50d0f582010-11-18 23:43:05 +00001032 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001033}
1034
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001035/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1036/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1037/// "c5", ...
1038static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001039 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1040 // but efficient.
1041 switch (Name.size()) {
1042 default: break;
1043 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001044 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001045 return -1;
1046 switch (Name[1]) {
1047 default: return -1;
1048 case '0': return 0;
1049 case '1': return 1;
1050 case '2': return 2;
1051 case '3': return 3;
1052 case '4': return 4;
1053 case '5': return 5;
1054 case '6': return 6;
1055 case '7': return 7;
1056 case '8': return 8;
1057 case '9': return 9;
1058 }
1059 break;
1060 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001061 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001062 return -1;
1063 switch (Name[2]) {
1064 default: return -1;
1065 case '0': return 10;
1066 case '1': return 11;
1067 case '2': return 12;
1068 case '3': return 13;
1069 case '4': return 14;
1070 case '5': return 15;
1071 }
1072 break;
1073 }
1074
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001075 return -1;
1076}
1077
Jim Grosbachf922c472011-02-12 01:34:40 +00001078/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001079/// token must be an Identifier when called, and if it is a coprocessor
1080/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001081ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1082tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001083 SMLoc S = Parser.getTok().getLoc();
1084 const AsmToken &Tok = Parser.getTok();
1085 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1086
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001087 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001088 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001089 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001090
1091 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001092 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001093 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001094}
1095
Jim Grosbachf922c472011-02-12 01:34:40 +00001096/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001097/// token must be an Identifier when called, and if it is a coprocessor
1098/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001099ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1100tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001101 SMLoc S = Parser.getTok().getLoc();
1102 const AsmToken &Tok = Parser.getTok();
1103 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1104
1105 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1106 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001107 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001108
1109 Parser.Lex(); // Eat identifier token.
1110 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001111 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001112}
1113
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001114/// Parse a register list, return it if successful else return null. The first
1115/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001116bool ARMAsmParser::
1117ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001118 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001119 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001120 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001121
Bill Wendling7729e062010-11-09 22:44:22 +00001122 // Read the rest of the registers in the list.
1123 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001124 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001125
Bill Wendling7729e062010-11-09 22:44:22 +00001126 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001127 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001128 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001129
Sean Callanan18b83232010-01-19 21:44:56 +00001130 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001131 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001132 if (RegTok.isNot(AsmToken::Identifier)) {
1133 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001134 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001135 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001136
Bill Wendling1d6a2652010-11-06 10:40:24 +00001137 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001138 if (RegNum == -1) {
1139 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001140 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001141 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001142
Bill Wendlinge7176102010-11-06 22:36:58 +00001143 if (IsRange) {
1144 int Reg = PrevRegNum;
1145 do {
1146 ++Reg;
1147 Registers.push_back(std::make_pair(Reg, RegLoc));
1148 } while (Reg != RegNum);
1149 } else {
1150 Registers.push_back(std::make_pair(RegNum, RegLoc));
1151 }
1152
1153 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001154 } while (Parser.getTok().is(AsmToken::Comma) ||
1155 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001156
1157 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001158 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001159 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1160 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001161 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001162 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001163
Bill Wendlinge7176102010-11-06 22:36:58 +00001164 SMLoc E = RCurlyTok.getLoc();
1165 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001166
Bill Wendlinge7176102010-11-06 22:36:58 +00001167 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001168 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001169 RI = Registers.begin(), RE = Registers.end();
1170
Bill Wendling7caebff2011-01-12 21:20:59 +00001171 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001172 bool EmittedWarning = false;
1173
Bill Wendling7caebff2011-01-12 21:20:59 +00001174 DenseMap<unsigned, bool> RegMap;
1175 RegMap[HighRegNum] = true;
1176
Bill Wendlinge7176102010-11-06 22:36:58 +00001177 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001178 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001179 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001180
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001181 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001182 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001183 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001184 }
1185
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001186 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001187 Warning(RegInfo.second,
1188 "register not in ascending order in register list");
1189
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001190 RegMap[Reg] = true;
1191 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001192 }
1193
Bill Wendling50d0f582010-11-18 23:43:05 +00001194 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1195 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001196}
1197
Jim Grosbachf922c472011-02-12 01:34:40 +00001198/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1199ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1200tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001201 SMLoc S = Parser.getTok().getLoc();
1202 const AsmToken &Tok = Parser.getTok();
1203 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1204 StringRef OptStr = Tok.getString();
1205
1206 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1207 .Case("sy", ARM_MB::SY)
1208 .Case("st", ARM_MB::ST)
1209 .Case("ish", ARM_MB::ISH)
1210 .Case("ishst", ARM_MB::ISHST)
1211 .Case("nsh", ARM_MB::NSH)
1212 .Case("nshst", ARM_MB::NSHST)
1213 .Case("osh", ARM_MB::OSH)
1214 .Case("oshst", ARM_MB::OSHST)
1215 .Default(~0U);
1216
1217 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001218 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001219
1220 Parser.Lex(); // Eat identifier token.
1221 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001222 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001223}
1224
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001225/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001226ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1227tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1228 SMLoc S = Parser.getTok().getLoc();
1229 const AsmToken &Tok = Parser.getTok();
1230 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1231 StringRef IFlagsStr = Tok.getString();
1232
1233 unsigned IFlags = 0;
1234 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1235 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1236 .Case("a", ARM_PROC::A)
1237 .Case("i", ARM_PROC::I)
1238 .Case("f", ARM_PROC::F)
1239 .Default(~0U);
1240
1241 // If some specific iflag is already set, it means that some letter is
1242 // present more than once, this is not acceptable.
1243 if (Flag == ~0U || (IFlags & Flag))
1244 return MatchOperand_NoMatch;
1245
1246 IFlags |= Flag;
1247 }
1248
1249 Parser.Lex(); // Eat identifier token.
1250 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1251 return MatchOperand_Success;
1252}
1253
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001254/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1255ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1256tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1257 SMLoc S = Parser.getTok().getLoc();
1258 const AsmToken &Tok = Parser.getTok();
1259 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1260 StringRef Mask = Tok.getString();
1261
1262 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1263 size_t Start = 0, Next = Mask.find('_');
1264 StringRef Flags = "";
1265 StringRef SpecReg = Mask.slice(Start, Next);
1266 if (Next != StringRef::npos)
1267 Flags = Mask.slice(Next+1, Mask.size());
1268
1269 // FlagsVal contains the complete mask:
1270 // 3-0: Mask
1271 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1272 unsigned FlagsVal = 0;
1273
1274 if (SpecReg == "apsr") {
1275 FlagsVal = StringSwitch<unsigned>(Flags)
1276 .Case("nzcvq", 0x8) // same as CPSR_c
1277 .Case("g", 0x4) // same as CPSR_s
1278 .Case("nzcvqg", 0xc) // same as CPSR_fs
1279 .Default(~0U);
1280
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001281 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001282 if (!Flags.empty())
1283 return MatchOperand_NoMatch;
1284 else
1285 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001286 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001287 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001288 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1289 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001290 for (int i = 0, e = Flags.size(); i != e; ++i) {
1291 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1292 .Case("c", 1)
1293 .Case("x", 2)
1294 .Case("s", 4)
1295 .Case("f", 8)
1296 .Default(~0U);
1297
1298 // If some specific flag is already set, it means that some letter is
1299 // present more than once, this is not acceptable.
1300 if (FlagsVal == ~0U || (FlagsVal & Flag))
1301 return MatchOperand_NoMatch;
1302 FlagsVal |= Flag;
1303 }
1304 } else // No match for special register.
1305 return MatchOperand_NoMatch;
1306
1307 // Special register without flags are equivalent to "fc" flags.
1308 if (!FlagsVal)
1309 FlagsVal = 0x9;
1310
1311 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1312 if (SpecReg == "spsr")
1313 FlagsVal |= 16;
1314
1315 Parser.Lex(); // Eat identifier token.
1316 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1317 return MatchOperand_Success;
1318}
1319
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001320/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1321ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1322tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001323 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001324
1325 if (ParseMemory(Operands, ARMII::AddrMode2))
1326 return MatchOperand_NoMatch;
1327
1328 return MatchOperand_Success;
1329}
1330
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001331/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1332ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1333tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1334 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1335
1336 if (ParseMemory(Operands, ARMII::AddrMode3))
1337 return MatchOperand_NoMatch;
1338
1339 return MatchOperand_Success;
1340}
1341
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001342/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1343/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1344/// when they refer multiple MIOperands inside a single one.
1345bool ARMAsmParser::
1346CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1347 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1348 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1349
1350 // Create a writeback register dummy placeholder.
1351 Inst.addOperand(MCOperand::CreateImm(0));
1352
1353 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1354 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1355 return true;
1356}
1357
1358/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1359/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1360/// when they refer multiple MIOperands inside a single one.
1361bool ARMAsmParser::
1362CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1363 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1364 // Create a writeback register dummy placeholder.
1365 Inst.addOperand(MCOperand::CreateImm(0));
1366 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1367 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1368 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1369 return true;
1370}
1371
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001372/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1373/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1374/// when they refer multiple MIOperands inside a single one.
1375bool ARMAsmParser::
1376CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1377 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1378 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1379
1380 // Create a writeback register dummy placeholder.
1381 Inst.addOperand(MCOperand::CreateImm(0));
1382
1383 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1384 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1385 return true;
1386}
1387
1388/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1389/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1390/// when they refer multiple MIOperands inside a single one.
1391bool ARMAsmParser::
1392CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1393 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1394 // Create a writeback register dummy placeholder.
1395 Inst.addOperand(MCOperand::CreateImm(0));
1396 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1397 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1398 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1399 return true;
1400}
1401
Bill Wendlinge7176102010-11-06 22:36:58 +00001402/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001403/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001404///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001405/// TODO Only preindexing and postindexing addressing are started, unindexed
1406/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001407bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001408ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1409 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001410 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001411 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001412 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001413 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001414 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001415
Sean Callanan18b83232010-01-19 21:44:56 +00001416 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001417 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1418 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001419 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001420 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001421 int BaseRegNum = TryParseRegister();
1422 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001423 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001424 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001425 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001426
Daniel Dunbar05710932011-01-18 05:34:17 +00001427 // The next token must either be a comma or a closing bracket.
1428 const AsmToken &Tok = Parser.getTok();
1429 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1430 return true;
1431
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001432 bool Preindexed = false;
1433 bool Postindexed = false;
1434 bool OffsetIsReg = false;
1435 bool Negative = false;
1436 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001437 ARMOperand *WBOp = 0;
1438 int OffsetRegNum = -1;
1439 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001440 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001441 const MCExpr *ShiftAmount = 0;
1442 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001443
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001444 // First look for preindexed address forms, that is after the "[Rn" we now
1445 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001446 if (Tok.is(AsmToken::Comma)) {
1447 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001448 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001449
Chris Lattner550276e2010-10-28 20:52:15 +00001450 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1451 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001452 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001453 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001454 if (RBracTok.isNot(AsmToken::RBrac)) {
1455 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001456 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001457 }
Sean Callanan76264762010-04-02 22:27:05 +00001458 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001459 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001460
Sean Callanan18b83232010-01-19 21:44:56 +00001461 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001462 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001463 // None of addrmode3 instruction uses "!"
1464 if (AddrMode == ARMII::AddrMode3)
1465 return true;
1466
Bill Wendling50d0f582010-11-18 23:43:05 +00001467 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1468 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001469 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001470 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001471 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1472 if (AddrMode == ARMII::AddrMode2)
1473 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001474 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001475 } else {
1476 // The "[Rn" we have so far was not followed by a comma.
1477
Jim Grosbach80eb2332010-10-29 17:41:25 +00001478 // If there's anything other than the right brace, this is a post indexing
1479 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001480 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001481 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001482
Sean Callanan18b83232010-01-19 21:44:56 +00001483 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001484
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001485 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001486 Postindexed = true;
1487 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001488
Chris Lattner550276e2010-10-28 20:52:15 +00001489 if (NextTok.isNot(AsmToken::Comma)) {
1490 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001491 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001492 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001493
Sean Callananb9a25b72010-01-19 20:27:46 +00001494 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001495
Chris Lattner550276e2010-10-28 20:52:15 +00001496 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001497 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001498 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001499 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001500 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001501 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001502
1503 // Force Offset to exist if used.
1504 if (!OffsetIsReg) {
1505 if (!Offset)
1506 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001507 } else {
1508 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1509 Error(E, "shift amount not supported");
1510 return true;
1511 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001512 }
1513
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001514 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1515 Offset, OffsetRegNum, OffsetRegShifted,
1516 ShiftType, ShiftAmount, Preindexed,
1517 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001518 if (WBOp)
1519 Operands.push_back(WBOp);
1520
1521 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001522}
1523
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001524/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1525/// we will parse the following (were +/- means that a plus or minus is
1526/// optional):
1527/// +/-Rm
1528/// +/-Rm, shift
1529/// #offset
1530/// we return false on success or an error otherwise.
1531bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001532 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001533 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001534 const MCExpr *&ShiftAmount,
1535 const MCExpr *&Offset,
1536 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001537 int &OffsetRegNum,
1538 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001539 Negative = false;
1540 OffsetRegShifted = false;
1541 OffsetIsReg = false;
1542 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001543 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001544 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001545 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001546 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001547 else if (NextTok.is(AsmToken::Minus)) {
1548 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001549 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001550 }
1551 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001552 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001553 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001554 SMLoc CurLoc = OffsetRegTok.getLoc();
1555 OffsetRegNum = TryParseRegister();
1556 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001557 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001558 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001559 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001560 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001561
Bill Wendling12f40e92010-11-06 10:51:53 +00001562 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001563 if (OffsetRegNum != -1) {
1564 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001565 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001566 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001567 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001568
Sean Callanan18b83232010-01-19 21:44:56 +00001569 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001570 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001571 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001572 OffsetRegShifted = true;
1573 }
1574 }
1575 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1576 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001577 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001578 if (HashTok.isNot(AsmToken::Hash))
1579 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001580
Sean Callananb9a25b72010-01-19 20:27:46 +00001581 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001582
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001583 if (getParser().ParseExpression(Offset))
1584 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001585 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001586 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001587 return false;
1588}
1589
1590/// ParseShift as one of these two:
1591/// ( lsl | lsr | asr | ror ) , # shift_amount
1592/// rrx
1593/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001594bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1595 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001596 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001597 if (Tok.isNot(AsmToken::Identifier))
1598 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001599 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001600 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001601 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001602 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001603 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001604 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001605 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001606 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001607 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001608 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001609 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001610 else
1611 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001612 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001613
1614 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001615 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001616 return false;
1617
1618 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001619 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001620 if (HashTok.isNot(AsmToken::Hash))
1621 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001622 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001623
1624 if (getParser().ParseExpression(ShiftAmount))
1625 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001626
1627 return false;
1628}
1629
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001630/// Parse a arm instruction operand. For now this parses the operand regardless
1631/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001632bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001633 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001634 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001635
1636 // Check if the current operand has a custom associated parser, if so, try to
1637 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001638 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1639 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001640 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001641 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1642 // there was a match, but an error occurred, in which case, just return that
1643 // the operand parsing failed.
1644 if (ResTy == MatchOperand_ParseFail)
1645 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001646
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001647 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001648 default:
1649 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001650 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001651 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001652 if (!TryParseRegisterWithWriteBack(Operands))
1653 return false;
Owen Anderson00828302011-03-18 22:50:18 +00001654 if (!TryParseShiftRegister(Operands))
1655 return false;
1656
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001657
1658 // Fall though for the Identifier case that is not a register or a
1659 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001660 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1661 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001662 // This was not a register so parse other operands that start with an
1663 // identifier (like labels) as expressions and create them as immediates.
1664 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001665 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001666 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001667 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001668 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001669 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1670 return false;
1671 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001672 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001673 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001674 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001675 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001676 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001677 // #42 -> immediate.
1678 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001679 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001680 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001681 const MCExpr *ImmVal;
1682 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001683 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001684 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001685 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1686 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001687 case AsmToken::Colon: {
1688 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001689 // FIXME: Check it's an expression prefix,
1690 // e.g. (FOO - :lower16:BAR) isn't legal.
1691 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001692 if (ParsePrefix(RefKind))
1693 return true;
1694
Evan Cheng75972122011-01-13 07:58:56 +00001695 const MCExpr *SubExprVal;
1696 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001697 return true;
1698
Evan Cheng75972122011-01-13 07:58:56 +00001699 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1700 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001701 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001702 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001703 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001704 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001705 }
1706}
1707
Evan Cheng75972122011-01-13 07:58:56 +00001708// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1709// :lower16: and :upper16:.
1710bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1711 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001712
1713 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001714 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001715 Parser.Lex(); // Eat ':'
1716
1717 if (getLexer().isNot(AsmToken::Identifier)) {
1718 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1719 return true;
1720 }
1721
1722 StringRef IDVal = Parser.getTok().getIdentifier();
1723 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001724 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001725 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001726 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001727 } else {
1728 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1729 return true;
1730 }
1731 Parser.Lex();
1732
1733 if (getLexer().isNot(AsmToken::Colon)) {
1734 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1735 return true;
1736 }
1737 Parser.Lex(); // Eat the last ':'
1738 return false;
1739}
1740
1741const MCExpr *
1742ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1743 MCSymbolRefExpr::VariantKind Variant) {
1744 // Recurse over the given expression, rebuilding it to apply the given variant
1745 // to the leftmost symbol.
1746 if (Variant == MCSymbolRefExpr::VK_None)
1747 return E;
1748
1749 switch (E->getKind()) {
1750 case MCExpr::Target:
1751 llvm_unreachable("Can't handle target expr yet");
1752 case MCExpr::Constant:
1753 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1754
1755 case MCExpr::SymbolRef: {
1756 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1757
1758 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1759 return 0;
1760
1761 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1762 }
1763
1764 case MCExpr::Unary:
1765 llvm_unreachable("Can't handle unary expressions yet");
1766
1767 case MCExpr::Binary: {
1768 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1769 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1770 const MCExpr *RHS = BE->getRHS();
1771 if (!LHS)
1772 return 0;
1773
1774 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1775 }
1776 }
1777
1778 assert(0 && "Invalid expression kind!");
1779 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001780}
1781
Daniel Dunbar352e1482011-01-11 15:59:50 +00001782/// \brief Given a mnemonic, split out possible predication code and carry
1783/// setting letters to form a canonical mnemonic and flags.
1784//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001785// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001786static StringRef SplitMnemonic(StringRef Mnemonic,
1787 unsigned &PredicationCode,
1788 bool &CarrySetting,
1789 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001790 PredicationCode = ARMCC::AL;
1791 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001792 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001793
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001794 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001795 //
1796 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001797 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1798 Mnemonic == "movs" ||
1799 Mnemonic == "svc" ||
1800 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1801 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1802 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1803 Mnemonic == "vclt" ||
1804 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1805 Mnemonic == "vcle" ||
1806 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1807 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbachd1f0bbe2011-06-27 20:59:10 +00001808 Mnemonic == "vqdmlal" || Mnemonic == "bics"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001809 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001810
Jim Grosbach3f00e312011-07-11 17:09:57 +00001811 // First, split out any predication code. Ignore mnemonics we know aren't
1812 // predicated but do have a carry-set and so weren't caught above.
1813 if (Mnemonic != "adcs") {
1814 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1815 .Case("eq", ARMCC::EQ)
1816 .Case("ne", ARMCC::NE)
1817 .Case("hs", ARMCC::HS)
1818 .Case("cs", ARMCC::HS)
1819 .Case("lo", ARMCC::LO)
1820 .Case("cc", ARMCC::LO)
1821 .Case("mi", ARMCC::MI)
1822 .Case("pl", ARMCC::PL)
1823 .Case("vs", ARMCC::VS)
1824 .Case("vc", ARMCC::VC)
1825 .Case("hi", ARMCC::HI)
1826 .Case("ls", ARMCC::LS)
1827 .Case("ge", ARMCC::GE)
1828 .Case("lt", ARMCC::LT)
1829 .Case("gt", ARMCC::GT)
1830 .Case("le", ARMCC::LE)
1831 .Case("al", ARMCC::AL)
1832 .Default(~0U);
1833 if (CC != ~0U) {
1834 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
1835 PredicationCode = CC;
1836 }
Bill Wendling52925b62010-10-29 23:50:21 +00001837 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001838
Daniel Dunbar352e1482011-01-11 15:59:50 +00001839 // Next, determine if we have a carry setting bit. We explicitly ignore all
1840 // the instructions we know end in 's'.
1841 if (Mnemonic.endswith("s") &&
1842 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1843 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1844 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1845 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1846 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1847 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1848 CarrySetting = true;
1849 }
1850
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001851 // The "cps" instruction can have a interrupt mode operand which is glued into
1852 // the mnemonic. Check if this is the case, split it and parse the imod op
1853 if (Mnemonic.startswith("cps")) {
1854 // Split out any imod code.
1855 unsigned IMod =
1856 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1857 .Case("ie", ARM_PROC::IE)
1858 .Case("id", ARM_PROC::ID)
1859 .Default(~0U);
1860 if (IMod != ~0U) {
1861 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1862 ProcessorIMod = IMod;
1863 }
1864 }
1865
Daniel Dunbar352e1482011-01-11 15:59:50 +00001866 return Mnemonic;
1867}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001868
1869/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1870/// inclusion of carry set or predication code operands.
1871//
1872// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001873void ARMAsmParser::
1874GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1875 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001876 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1877 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1878 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1879 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001880 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001881 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1882 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001883 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00001884 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001885 CanAcceptCarrySet = true;
1886 } else {
1887 CanAcceptCarrySet = false;
1888 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001889
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001890 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1891 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1892 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1893 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001894 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001895 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001896 CanAcceptPredicationCode = false;
1897 } else {
1898 CanAcceptPredicationCode = true;
1899 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001900
Evan Chengebdeeab2011-07-08 01:53:10 +00001901 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001902 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001903 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001904 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001905}
1906
1907/// Parse an arm instruction mnemonic followed by its operands.
1908bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1909 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1910 // Create the leading tokens for the mnemonic, split by '.' characters.
1911 size_t Start = 0, Next = Name.find('.');
1912 StringRef Head = Name.slice(Start, Next);
1913
Daniel Dunbar352e1482011-01-11 15:59:50 +00001914 // Split out the predication code and carry setting flag from the mnemonic.
1915 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001916 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001917 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001918 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1919 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001920
Chris Lattner3a697562010-10-28 17:20:03 +00001921 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001922
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001923 // Next, add the CCOut and ConditionCode operands, if needed.
1924 //
1925 // For mnemonics which can ever incorporate a carry setting bit or predication
1926 // code, our matching model involves us always generating CCOut and
1927 // ConditionCode operands to match the mnemonic "as written" and then we let
1928 // the matcher deal with finding the right instruction or generating an
1929 // appropriate error.
1930 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1931 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1932
1933 // Add the carry setting operand, if necessary.
1934 //
1935 // FIXME: It would be awesome if we could somehow invent a location such that
1936 // match errors on this operand would print a nice diagnostic about how the
1937 // 's' character in the mnemonic resulted in a CCOut operand.
1938 if (CanAcceptCarrySet) {
1939 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1940 NameLoc));
1941 } else {
1942 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1943 // misspelled another mnemonic).
1944
1945 // FIXME: Issue a nice error.
1946 }
1947
1948 // Add the predication code operand, if necessary.
1949 if (CanAcceptPredicationCode) {
1950 Operands.push_back(ARMOperand::CreateCondCode(
1951 ARMCC::CondCodes(PredicationCode), NameLoc));
1952 } else {
1953 // This mnemonic can't ever accept a predication code, but the user wrote
1954 // one (or misspelled another mnemonic).
1955
1956 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001957 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001958
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001959 // Add the processor imod operand, if necessary.
1960 if (ProcessorIMod) {
1961 Operands.push_back(ARMOperand::CreateImm(
1962 MCConstantExpr::Create(ProcessorIMod, getContext()),
1963 NameLoc, NameLoc));
1964 } else {
1965 // This mnemonic can't ever accept a imod, but the user wrote
1966 // one (or misspelled another mnemonic).
1967
1968 // FIXME: Issue a nice error.
1969 }
1970
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001971 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001972 while (Next != StringRef::npos) {
1973 Start = Next;
1974 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001975 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001976
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001977 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001978 }
1979
1980 // Read the remaining operands.
1981 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001982 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001983 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001984 Parser.EatToEndOfStatement();
1985 return true;
1986 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001987
1988 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001989 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001990
1991 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001992 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001993 Parser.EatToEndOfStatement();
1994 return true;
1995 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001996 }
1997 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001998
Chris Lattnercbf8a982010-09-11 16:18:25 +00001999 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2000 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002001 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002002 }
Bill Wendling146018f2010-11-06 21:42:12 +00002003
Chris Lattner34e53142010-09-08 05:10:46 +00002004 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00002005 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002006}
2007
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002008bool ARMAsmParser::
2009MatchAndEmitInstruction(SMLoc IDLoc,
2010 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2011 MCStreamer &Out) {
2012 MCInst Inst;
2013 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002014 MatchResultTy MatchResult, MatchResult2;
2015 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2016 if (MatchResult != Match_Success) {
2017 // If we get a Match_InvalidOperand it might be some arithmetic instruction
2018 // that does not update the condition codes. So try adding a CCOut operand
2019 // with a value of reg0.
2020 if (MatchResult == Match_InvalidOperand) {
2021 Operands.insert(Operands.begin() + 1,
2022 ARMOperand::CreateCCOut(0,
2023 ((ARMOperand*)Operands[0])->getStartLoc()));
2024 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2025 if (MatchResult2 == Match_Success)
2026 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002027 else {
2028 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002029 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002030 delete CCOut;
2031 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002032 }
2033 // If we get a Match_MnemonicFail it might be some arithmetic instruction
2034 // that updates the condition codes if it ends in 's'. So see if the
2035 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
2036 // operand with a value of CPSR.
Evan Chengeb0caa12011-07-08 22:49:55 +00002037 else if (MatchResult == Match_MnemonicFail) {
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002038 // Get the instruction mnemonic, which is the first token.
2039 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
2040 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
2041 // removed the 's' from the mnemonic for matching.
2042 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
2043 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002044 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2045 Operands.erase(Operands.begin());
2046 delete OldMnemonic;
2047 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002048 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
2049 Operands.insert(Operands.begin() + 1,
2050 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2051 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2052 if (MatchResult2 == Match_Success)
2053 MatchResult = Match_Success;
2054 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002055 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2056 Operands.erase(Operands.begin());
2057 delete OldMnemonic;
2058 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002059 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002060 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2061 Operands.erase(Operands.begin() + 1);
2062 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002063 }
2064 }
2065 }
2066 }
2067 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002068 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002069 Out.EmitInstruction(Inst);
2070 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002071 case Match_MissingFeature:
2072 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2073 return true;
2074 case Match_InvalidOperand: {
2075 SMLoc ErrorLoc = IDLoc;
2076 if (ErrorInfo != ~0U) {
2077 if (ErrorInfo >= Operands.size())
2078 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002079
Chris Lattnere73d4f82010-10-28 21:41:58 +00002080 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2081 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2082 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002083
Chris Lattnere73d4f82010-10-28 21:41:58 +00002084 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002085 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002086 case Match_MnemonicFail:
2087 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002088 case Match_ConversionFail:
2089 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002090 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002091
Eric Christopherc223e2b2010-10-29 09:26:59 +00002092 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002093 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002094}
2095
Kevin Enderby515d5092009-10-15 20:48:48 +00002096/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002097bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2098 StringRef IDVal = DirectiveID.getIdentifier();
2099 if (IDVal == ".word")
2100 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002101 else if (IDVal == ".thumb")
2102 return ParseDirectiveThumb(DirectiveID.getLoc());
2103 else if (IDVal == ".thumb_func")
2104 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2105 else if (IDVal == ".code")
2106 return ParseDirectiveCode(DirectiveID.getLoc());
2107 else if (IDVal == ".syntax")
2108 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002109 return true;
2110}
2111
2112/// ParseDirectiveWord
2113/// ::= .word [ expression (, expression)* ]
2114bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2115 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2116 for (;;) {
2117 const MCExpr *Value;
2118 if (getParser().ParseExpression(Value))
2119 return true;
2120
Chris Lattneraaec2052010-01-19 19:46:13 +00002121 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002122
2123 if (getLexer().is(AsmToken::EndOfStatement))
2124 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002125
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002126 // FIXME: Improve diagnostic.
2127 if (getLexer().isNot(AsmToken::Comma))
2128 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002129 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002130 }
2131 }
2132
Sean Callananb9a25b72010-01-19 20:27:46 +00002133 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002134 return false;
2135}
2136
Kevin Enderby515d5092009-10-15 20:48:48 +00002137/// ParseDirectiveThumb
2138/// ::= .thumb
2139bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2140 if (getLexer().isNot(AsmToken::EndOfStatement))
2141 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002142 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002143
2144 // TODO: set thumb mode
2145 // TODO: tell the MC streamer the mode
2146 // getParser().getStreamer().Emit???();
2147 return false;
2148}
2149
2150/// ParseDirectiveThumbFunc
2151/// ::= .thumbfunc symbol_name
2152bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002153 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2154 bool isMachO = MAI.hasSubsectionsViaSymbols();
2155 StringRef Name;
2156
2157 // Darwin asm has function name after .thumb_func direction
2158 // ELF doesn't
2159 if (isMachO) {
2160 const AsmToken &Tok = Parser.getTok();
2161 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2162 return Error(L, "unexpected token in .thumb_func directive");
2163 Name = Tok.getString();
2164 Parser.Lex(); // Consume the identifier token.
2165 }
2166
Kevin Enderby515d5092009-10-15 20:48:48 +00002167 if (getLexer().isNot(AsmToken::EndOfStatement))
2168 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002169 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002170
Rafael Espindola64695402011-05-16 16:17:21 +00002171 // FIXME: assuming function name will be the line following .thumb_func
2172 if (!isMachO) {
2173 Name = Parser.getTok().getString();
2174 }
2175
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002176 // Mark symbol as a thumb symbol.
2177 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2178 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002179 return false;
2180}
2181
2182/// ParseDirectiveSyntax
2183/// ::= .syntax unified | divided
2184bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002185 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002186 if (Tok.isNot(AsmToken::Identifier))
2187 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002188 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002189 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002190 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002191 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002192 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002193 else
2194 return Error(L, "unrecognized syntax mode in .syntax directive");
2195
2196 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002197 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002198 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002199
2200 // TODO tell the MC streamer the mode
2201 // getParser().getStreamer().Emit???();
2202 return false;
2203}
2204
2205/// ParseDirectiveCode
2206/// ::= .code 16 | 32
2207bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002208 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002209 if (Tok.isNot(AsmToken::Integer))
2210 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002211 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002212 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002213 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002214 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002215 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002216 else
2217 return Error(L, "invalid operand to .code directive");
2218
2219 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002220 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002221 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002222
Evan Cheng32869202011-07-08 22:36:29 +00002223 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002224 if (!isThumb())
2225 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002226 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002227 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002228 if (isThumb())
2229 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002230 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002231 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002232
Kevin Enderby515d5092009-10-15 20:48:48 +00002233 return false;
2234}
2235
Sean Callanan90b70972010-04-07 20:29:34 +00002236extern "C" void LLVMInitializeARMAsmLexer();
2237
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002238/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002239extern "C" void LLVMInitializeARMAsmParser() {
2240 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2241 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002242 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002243}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002244
Chris Lattner0692ee62010-09-06 19:11:01 +00002245#define GET_REGISTER_MATCHER
2246#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002247#include "ARMGenAsmMatcher.inc"