blob: 752e90af775bb07903ad4b2beba543b0d862f8d9 [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ARM.h"
Bill Wendling92b5a2e2010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Evan Cheng75972122011-01-13 07:58:56 +000012#include "ARMMCExpr.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000013#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000014#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
17#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Rafael Espindola64695402011-05-16 16:17:21 +000018#include "llvm/MC/MCAsmInfo.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000019#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000020#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000023#include "llvm/MC/MCSubtargetInfo.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000024#include "llvm/Target/TargetRegistry.h"
25#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000027#include "llvm/Support/raw_ostream.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000028#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000030#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000031#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000032#include "llvm/ADT/Twine.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000033
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000034using namespace llvm;
35
Chris Lattner3a697562010-10-28 17:20:03 +000036namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000037
38class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000039
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000040class ARMAsmParser : public TargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000041 MCSubtargetInfo &STI;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042 MCAsmParser &Parser;
43
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000044 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
46
47 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
49
Chris Lattnere5658fa2010-10-30 04:09:10 +000050 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000051 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000052 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach19906722011-07-13 18:49:30 +000053 int TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000054 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000055 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
56 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000057 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000058 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000059 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
60 MCSymbolRefExpr::VariantKind Variant);
61
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000062
Kevin Enderby9c41fa82009-10-30 22:55:57 +000063 bool ParseMemoryOffsetReg(bool &Negative,
64 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000065 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 const MCExpr *&ShiftAmount,
67 const MCExpr *&Offset,
68 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000069 int &OffsetRegNum,
70 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000071 bool ParseShift(enum ARM_AM::ShiftOpc &St,
72 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000073 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000074 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000075 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000076 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveSyntax(SMLoc L);
78
Chris Lattner7036f8b2010-09-29 01:42:58 +000079 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000080 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000081 MCStreamer &Out);
Jim Grosbach5f160572011-07-19 20:10:31 +000082 StringRef SplitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
83 bool &CarrySetting, unsigned &ProcessorIMod);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000084 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
85 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000086
Evan Chengebdeeab2011-07-08 01:53:10 +000087 bool isThumb() const {
88 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000089 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000090 }
Evan Chengebdeeab2011-07-08 01:53:10 +000091 bool isThumbOne() const {
Evan Chengffc0e732011-07-09 05:47:46 +000092 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000093 }
Evan Cheng32869202011-07-08 22:36:29 +000094 void SwitchMode() {
Evan Chengffc0e732011-07-09 05:47:46 +000095 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
96 setAvailableFeatures(FB);
Evan Cheng32869202011-07-08 22:36:29 +000097 }
Evan Chengebdeeab2011-07-08 01:53:10 +000098
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000099 /// @name Auto-generated Match Functions
100 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000101
Chris Lattner0692ee62010-09-06 19:11:01 +0000102#define GET_ASSEMBLER_HEADER
103#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000104
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000105 /// }
106
Jim Grosbachf922c472011-02-12 01:34:40 +0000107 OperandMatchResultTy tryParseCoprocNumOperand(
108 SmallVectorImpl<MCParsedAsmOperand*>&);
109 OperandMatchResultTy tryParseCoprocRegOperand(
110 SmallVectorImpl<MCParsedAsmOperand*>&);
111 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000112 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000113 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000114 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000115 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000116 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000117 OperandMatchResultTy tryParseMemMode2Operand(
118 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000119 OperandMatchResultTy tryParseMemMode3Operand(
120 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000121
122 // Asm Match Converter Methods
123 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
124 const SmallVectorImpl<MCParsedAsmOperand*> &);
125 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
126 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000127 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
128 const SmallVectorImpl<MCParsedAsmOperand*> &);
129 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
130 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000131
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000132public:
Evan Chengffc0e732011-07-09 05:47:46 +0000133 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
134 : TargetAsmParser(), STI(_STI), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000135 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000136
Evan Chengebdeeab2011-07-08 01:53:10 +0000137 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000138 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evan Chengebdeeab2011-07-08 01:53:10 +0000139 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000140
Benjamin Kramer38e59892010-07-14 22:38:02 +0000141 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000142 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000143 virtual bool ParseDirective(AsmToken DirectiveID);
144};
Jim Grosbach16c74252010-10-29 14:46:02 +0000145} // end anonymous namespace
146
Chris Lattner3a697562010-10-28 17:20:03 +0000147namespace {
148
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000149/// ARMOperand - Instances of this class represent a parsed ARM machine
150/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000151class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000152 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000153 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000154 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000155 CoprocNum,
156 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000157 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000158 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000159 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000160 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000161 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000162 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000163 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000164 DPRRegisterList,
165 SPRRegisterList,
Jim Grosbache8606dc2011-07-13 17:50:29 +0000166 ShiftedRegister,
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;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000228 unsigned Imm;
Owen Anderson00828302011-03-18 22:50:18 +0000229 } Shift;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000230 struct {
231 ARM_AM::ShiftOpc ShiftTy;
232 unsigned SrcReg;
233 unsigned ShiftReg;
234 unsigned ShiftImm;
235 } ShiftedReg;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000236 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000237
Bill Wendling146018f2010-11-06 21:42:12 +0000238 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
239public:
Sean Callanan76264762010-04-02 22:27:05 +0000240 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
241 Kind = o.Kind;
242 StartLoc = o.StartLoc;
243 EndLoc = o.EndLoc;
244 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000245 case CondCode:
246 CC = o.CC;
247 break;
Sean Callanan76264762010-04-02 22:27:05 +0000248 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000249 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000250 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000251 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000252 case Register:
253 Reg = o.Reg;
254 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000255 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000256 case DPRRegisterList:
257 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000258 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000259 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000260 case CoprocNum:
261 case CoprocReg:
262 Cop = o.Cop;
263 break;
Sean Callanan76264762010-04-02 22:27:05 +0000264 case Immediate:
265 Imm = o.Imm;
266 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000267 case MemBarrierOpt:
268 MBOpt = o.MBOpt;
269 break;
Sean Callanan76264762010-04-02 22:27:05 +0000270 case Memory:
271 Mem = o.Mem;
272 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000273 case MSRMask:
274 MMask = o.MMask;
275 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000276 case ProcIFlags:
277 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000278 break;
279 case Shifter:
280 Shift = o.Shift;
281 break;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000282 case ShiftedRegister:
283 ShiftedReg = o.ShiftedReg;
284 break;
Sean Callanan76264762010-04-02 22:27:05 +0000285 }
286 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000287
Sean Callanan76264762010-04-02 22:27:05 +0000288 /// getStartLoc - Get the location of the first token of this operand.
289 SMLoc getStartLoc() const { return StartLoc; }
290 /// getEndLoc - Get the location of the last token of this operand.
291 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000292
Daniel Dunbar8462b302010-08-11 06:36:53 +0000293 ARMCC::CondCodes getCondCode() const {
294 assert(Kind == CondCode && "Invalid access!");
295 return CC.Val;
296 }
297
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000298 unsigned getCoproc() const {
299 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
300 return Cop.Val;
301 }
302
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000303 StringRef getToken() const {
304 assert(Kind == Token && "Invalid access!");
305 return StringRef(Tok.Data, Tok.Length);
306 }
307
308 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000309 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000310 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000311 }
312
Bill Wendling5fa22a12010-11-09 23:28:44 +0000313 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000314 assert((Kind == RegisterList || Kind == DPRRegisterList ||
315 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000316 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000317 }
318
Kevin Enderbycfe07242009-10-13 22:19:02 +0000319 const MCExpr *getImm() const {
320 assert(Kind == Immediate && "Invalid access!");
321 return Imm.Val;
322 }
323
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000324 ARM_MB::MemBOpt getMemBarrierOpt() const {
325 assert(Kind == MemBarrierOpt && "Invalid access!");
326 return MBOpt.Val;
327 }
328
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000329 ARM_PROC::IFlags getProcIFlags() const {
330 assert(Kind == ProcIFlags && "Invalid access!");
331 return IFlags.Val;
332 }
333
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000334 unsigned getMSRMask() const {
335 assert(Kind == MSRMask && "Invalid access!");
336 return MMask.Val;
337 }
338
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000339 /// @name Memory Operand Accessors
340 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000341 ARMII::AddrMode getMemAddrMode() const {
342 return Mem.AddrMode;
343 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000344 unsigned getMemBaseRegNum() const {
345 return Mem.BaseRegNum;
346 }
347 unsigned getMemOffsetRegNum() const {
348 assert(Mem.OffsetIsReg && "Invalid access!");
349 return Mem.Offset.RegNum;
350 }
351 const MCExpr *getMemOffset() const {
352 assert(!Mem.OffsetIsReg && "Invalid access!");
353 return Mem.Offset.Value;
354 }
355 unsigned getMemOffsetRegShifted() const {
356 assert(Mem.OffsetIsReg && "Invalid access!");
357 return Mem.OffsetRegShifted;
358 }
359 const MCExpr *getMemShiftAmount() const {
360 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
361 return Mem.ShiftAmount;
362 }
Owen Anderson00828302011-03-18 22:50:18 +0000363 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000364 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
365 return Mem.ShiftType;
366 }
367 bool getMemPreindexed() const { return Mem.Preindexed; }
368 bool getMemPostindexed() const { return Mem.Postindexed; }
369 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
370 bool getMemNegative() const { return Mem.Negative; }
371 bool getMemWriteback() const { return Mem.Writeback; }
372
373 /// @}
374
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000375 bool isCoprocNum() const { return Kind == CoprocNum; }
376 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000377 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000378 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000379 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000380 bool isImm0_255() const {
381 if (Kind != Immediate)
382 return false;
383 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
384 if (!CE) return false;
385 int64_t Value = CE->getValue();
386 return Value >= 0 && Value < 256;
387 }
Jim Grosbach83ab0702011-07-13 22:01:08 +0000388 bool isImm0_7() const {
389 if (Kind != Immediate)
390 return false;
391 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
392 if (!CE) return false;
393 int64_t Value = CE->getValue();
394 return Value >= 0 && Value < 8;
395 }
396 bool isImm0_15() const {
397 if (Kind != Immediate)
398 return false;
399 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
400 if (!CE) return false;
401 int64_t Value = CE->getValue();
402 return Value >= 0 && Value < 16;
403 }
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000404 bool isImm0_65535() const {
405 if (Kind != Immediate)
406 return false;
407 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
408 if (!CE) return false;
409 int64_t Value = CE->getValue();
410 return Value >= 0 && Value < 65536;
411 }
Jim Grosbachffa32252011-07-19 19:13:28 +0000412 bool isImm0_65535Expr() const {
413 if (Kind != Immediate)
414 return false;
415 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
416 // If it's not a constant expression, it'll generate a fixup and be
417 // handled later.
418 if (!CE) return true;
419 int64_t Value = CE->getValue();
420 return Value >= 0 && Value < 65536;
421 }
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000422 bool isARMSOImm() const {
423 if (Kind != Immediate)
424 return false;
425 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
426 if (!CE) return false;
427 int64_t Value = CE->getValue();
428 return ARM_AM::getSOImmVal(Value) != -1;
429 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000430 bool isT2SOImm() const {
431 if (Kind != Immediate)
432 return false;
433 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
434 if (!CE) return false;
435 int64_t Value = CE->getValue();
436 return ARM_AM::getT2SOImmVal(Value) != -1;
437 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000438 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000439 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000440 bool isDPRRegList() const { return Kind == DPRRegisterList; }
441 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000442 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000443 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000444 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000445 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000446 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000447 bool isMemMode2() const {
448 if (getMemAddrMode() != ARMII::AddrMode2)
449 return false;
450
451 if (getMemOffsetIsReg())
452 return true;
453
454 if (getMemNegative() &&
455 !(getMemPostindexed() || getMemPreindexed()))
456 return false;
457
458 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
459 if (!CE) return false;
460 int64_t Value = CE->getValue();
461
462 // The offset must be in the range 0-4095 (imm12).
463 if (Value > 4095 || Value < -4095)
464 return false;
465
466 return true;
467 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000468 bool isMemMode3() const {
469 if (getMemAddrMode() != ARMII::AddrMode3)
470 return false;
471
472 if (getMemOffsetIsReg()) {
473 if (getMemOffsetRegShifted())
474 return false; // No shift with offset reg allowed
475 return true;
476 }
477
478 if (getMemNegative() &&
479 !(getMemPostindexed() || getMemPreindexed()))
480 return false;
481
482 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
483 if (!CE) return false;
484 int64_t Value = CE->getValue();
485
486 // The offset must be in the range 0-255 (imm8).
487 if (Value > 255 || Value < -255)
488 return false;
489
490 return true;
491 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000492 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000493 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
494 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000495 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000496
Daniel Dunbar4b462672011-01-18 05:55:27 +0000497 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000498 if (!CE) return false;
499
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000500 // The offset must be a multiple of 4 in the range 0-1020.
501 int64_t Value = CE->getValue();
502 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
503 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000504 bool isMemMode7() const {
505 if (!isMemory() ||
506 getMemPreindexed() ||
507 getMemPostindexed() ||
508 getMemOffsetIsReg() ||
509 getMemNegative() ||
510 getMemWriteback())
511 return false;
512
513 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
514 if (!CE) return false;
515
516 if (CE->getValue())
517 return false;
518
519 return true;
520 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000521 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000522 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000523 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000524 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000525 }
526 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000527 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000528 return false;
529
Daniel Dunbar4b462672011-01-18 05:55:27 +0000530 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000531 if (!CE) return false;
532
533 // The offset must be a multiple of 4 in the range 0-124.
534 uint64_t Value = CE->getValue();
535 return ((Value & 0x3) == 0 && Value <= 124);
536 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000537 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000538 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000539
540 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000541 // Add as immediates when possible. Null MCExpr = 0.
542 if (Expr == 0)
543 Inst.addOperand(MCOperand::CreateImm(0));
544 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000545 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
546 else
547 Inst.addOperand(MCOperand::CreateExpr(Expr));
548 }
549
Daniel Dunbar8462b302010-08-11 06:36:53 +0000550 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000551 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000552 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000553 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
554 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000555 }
556
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000557 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
558 assert(N == 1 && "Invalid number of operands!");
559 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
560 }
561
562 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
563 assert(N == 1 && "Invalid number of operands!");
564 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
565 }
566
Jim Grosbachd67641b2010-12-06 18:21:12 +0000567 void addCCOutOperands(MCInst &Inst, unsigned N) const {
568 assert(N == 1 && "Invalid number of operands!");
569 Inst.addOperand(MCOperand::CreateReg(getReg()));
570 }
571
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000572 void addRegOperands(MCInst &Inst, unsigned N) const {
573 assert(N == 1 && "Invalid number of operands!");
574 Inst.addOperand(MCOperand::CreateReg(getReg()));
575 }
576
Jim Grosbache8606dc2011-07-13 17:50:29 +0000577 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
578 assert(N == 3 && "Invalid number of operands!");
579 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
580 assert((ShiftedReg.ShiftReg == 0 ||
581 ARM_AM::getSORegOffset(ShiftedReg.ShiftImm) == 0) &&
582 "Invalid shifted register operand!");
583 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
584 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
585 Inst.addOperand(MCOperand::CreateImm(
586 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
587 }
588
Owen Anderson00828302011-03-18 22:50:18 +0000589 void addShifterOperands(MCInst &Inst, unsigned N) const {
590 assert(N == 1 && "Invalid number of operands!");
591 Inst.addOperand(MCOperand::CreateImm(
592 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
593 }
594
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000595 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000596 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000597 const SmallVectorImpl<unsigned> &RegList = getRegList();
598 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000599 I = RegList.begin(), E = RegList.end(); I != E; ++I)
600 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000601 }
602
Bill Wendling0f630752010-11-17 04:32:08 +0000603 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
604 addRegListOperands(Inst, N);
605 }
606
607 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
608 addRegListOperands(Inst, N);
609 }
610
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000611 void addImmOperands(MCInst &Inst, unsigned N) const {
612 assert(N == 1 && "Invalid number of operands!");
613 addExpr(Inst, getImm());
614 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000615
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000616 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
617 assert(N == 1 && "Invalid number of operands!");
618 addExpr(Inst, getImm());
619 }
620
Jim Grosbach83ab0702011-07-13 22:01:08 +0000621 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
622 assert(N == 1 && "Invalid number of operands!");
623 addExpr(Inst, getImm());
624 }
625
626 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
627 assert(N == 1 && "Invalid number of operands!");
628 addExpr(Inst, getImm());
629 }
630
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000631 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
632 assert(N == 1 && "Invalid number of operands!");
633 addExpr(Inst, getImm());
634 }
635
Jim Grosbachffa32252011-07-19 19:13:28 +0000636 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
637 assert(N == 1 && "Invalid number of operands!");
638 addExpr(Inst, getImm());
639 }
640
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000641 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
642 assert(N == 1 && "Invalid number of operands!");
643 addExpr(Inst, getImm());
644 }
645
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000646 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
647 assert(N == 1 && "Invalid number of operands!");
648 addExpr(Inst, getImm());
649 }
650
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000651 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
652 assert(N == 1 && "Invalid number of operands!");
653 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
654 }
655
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000656 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
657 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
658 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
659
660 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000661 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000662 assert((CE || CE->getValue() == 0) &&
663 "No offset operand support in mode 7");
664 }
665
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000666 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
667 assert(isMemMode2() && "Invalid mode or number of operands!");
668 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
669 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
670
671 if (getMemOffsetIsReg()) {
672 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
673
674 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
675 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
676 int64_t ShiftAmount = 0;
677
678 if (getMemOffsetRegShifted()) {
679 ShOpc = getMemShiftType();
680 const MCConstantExpr *CE =
681 dyn_cast<MCConstantExpr>(getMemShiftAmount());
682 ShiftAmount = CE->getValue();
683 }
684
685 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
686 ShOpc, IdxMode)));
687 return;
688 }
689
690 // Create a operand placeholder to always yield the same number of operands.
691 Inst.addOperand(MCOperand::CreateReg(0));
692
693 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
694 // the difference?
695 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
696 assert(CE && "Non-constant mode 2 offset operand!");
697 int64_t Offset = CE->getValue();
698
699 if (Offset >= 0)
700 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
701 Offset, ARM_AM::no_shift, IdxMode)));
702 else
703 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
704 -Offset, ARM_AM::no_shift, IdxMode)));
705 }
706
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000707 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
708 assert(isMemMode3() && "Invalid mode or number of operands!");
709 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
710 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
711
712 if (getMemOffsetIsReg()) {
713 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
714
715 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
716 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
717 IdxMode)));
718 return;
719 }
720
721 // Create a operand placeholder to always yield the same number of operands.
722 Inst.addOperand(MCOperand::CreateReg(0));
723
724 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
725 // the difference?
726 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
727 assert(CE && "Non-constant mode 3 offset operand!");
728 int64_t Offset = CE->getValue();
729
730 if (Offset >= 0)
731 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
732 Offset, IdxMode)));
733 else
734 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
735 -Offset, IdxMode)));
736 }
737
Chris Lattner14b93852010-10-29 00:27:31 +0000738 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
739 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000740
Daniel Dunbar4b462672011-01-18 05:55:27 +0000741 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
742 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000743
Jim Grosbach80eb2332010-10-29 17:41:25 +0000744 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
745 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000746 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000747 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000748
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000749 // The MCInst offset operand doesn't include the low two bits (like
750 // the instruction encoding).
751 int64_t Offset = CE->getValue() / 4;
752 if (Offset >= 0)
753 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
754 Offset)));
755 else
756 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
757 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000758 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000759
Bill Wendlingf4caf692010-12-14 03:36:38 +0000760 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
761 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000762 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
763 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000764 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000765
Bill Wendlingf4caf692010-12-14 03:36:38 +0000766 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
767 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000768 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
769 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000770 assert(CE && "Non-constant mode offset operand!");
771 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000772 }
773
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000774 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
775 assert(N == 1 && "Invalid number of operands!");
776 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
777 }
778
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000779 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
780 assert(N == 1 && "Invalid number of operands!");
781 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
782 }
783
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000784 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000785
Chris Lattner3a697562010-10-28 17:20:03 +0000786 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
787 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000788 Op->CC.Val = CC;
789 Op->StartLoc = S;
790 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000791 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000792 }
793
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000794 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
795 ARMOperand *Op = new ARMOperand(CoprocNum);
796 Op->Cop.Val = CopVal;
797 Op->StartLoc = S;
798 Op->EndLoc = S;
799 return Op;
800 }
801
802 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
803 ARMOperand *Op = new ARMOperand(CoprocReg);
804 Op->Cop.Val = CopVal;
805 Op->StartLoc = S;
806 Op->EndLoc = S;
807 return Op;
808 }
809
Jim Grosbachd67641b2010-12-06 18:21:12 +0000810 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
811 ARMOperand *Op = new ARMOperand(CCOut);
812 Op->Reg.RegNum = RegNum;
813 Op->StartLoc = S;
814 Op->EndLoc = S;
815 return Op;
816 }
817
Chris Lattner3a697562010-10-28 17:20:03 +0000818 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
819 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000820 Op->Tok.Data = Str.data();
821 Op->Tok.Length = Str.size();
822 Op->StartLoc = S;
823 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000824 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000825 }
826
Bill Wendling50d0f582010-11-18 23:43:05 +0000827 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000828 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000829 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000830 Op->StartLoc = S;
831 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000832 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000833 }
834
Jim Grosbache8606dc2011-07-13 17:50:29 +0000835 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
836 unsigned SrcReg,
837 unsigned ShiftReg,
838 unsigned ShiftImm,
839 SMLoc S, SMLoc E) {
840 ARMOperand *Op = new ARMOperand(ShiftedRegister);
841 Op->ShiftedReg.ShiftTy = ShTy;
842 Op->ShiftedReg.SrcReg = SrcReg;
843 Op->ShiftedReg.ShiftReg = ShiftReg;
844 Op->ShiftedReg.ShiftImm = ShiftImm;
845 Op->StartLoc = S;
846 Op->EndLoc = E;
847 return Op;
848 }
849
Owen Anderson00828302011-03-18 22:50:18 +0000850 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
851 SMLoc S, SMLoc E) {
852 ARMOperand *Op = new ARMOperand(Shifter);
853 Op->Shift.ShiftTy = ShTy;
854 Op->StartLoc = S;
855 Op->EndLoc = E;
856 return Op;
857 }
858
Bill Wendling7729e062010-11-09 22:44:22 +0000859 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000860 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000861 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000862 KindTy Kind = RegisterList;
863
864 if (ARM::DPRRegClass.contains(Regs.front().first))
865 Kind = DPRRegisterList;
866 else if (ARM::SPRRegClass.contains(Regs.front().first))
867 Kind = SPRRegisterList;
868
869 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000870 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000871 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000872 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000873 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000874 Op->StartLoc = StartLoc;
875 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000876 return Op;
877 }
878
Chris Lattner3a697562010-10-28 17:20:03 +0000879 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
880 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000881 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000882 Op->StartLoc = S;
883 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000884 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000885 }
886
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000887 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
888 bool OffsetIsReg, const MCExpr *Offset,
889 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000890 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000891 const MCExpr *ShiftAmount, bool Preindexed,
892 bool Postindexed, bool Negative, bool Writeback,
893 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000894 assert((OffsetRegNum == -1 || OffsetIsReg) &&
895 "OffsetRegNum must imply OffsetIsReg!");
896 assert((!OffsetRegShifted || OffsetIsReg) &&
897 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000898 assert((Offset || OffsetIsReg) &&
899 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000900 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
901 "Cannot have shift amount without shifted register offset!");
902 assert((!Offset || !OffsetIsReg) &&
903 "Cannot have expression offset and register offset!");
904
Chris Lattner3a697562010-10-28 17:20:03 +0000905 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000906 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000907 Op->Mem.BaseRegNum = BaseRegNum;
908 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000909 if (OffsetIsReg)
910 Op->Mem.Offset.RegNum = OffsetRegNum;
911 else
912 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000913 Op->Mem.OffsetRegShifted = OffsetRegShifted;
914 Op->Mem.ShiftType = ShiftType;
915 Op->Mem.ShiftAmount = ShiftAmount;
916 Op->Mem.Preindexed = Preindexed;
917 Op->Mem.Postindexed = Postindexed;
918 Op->Mem.Negative = Negative;
919 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000920
Sean Callanan76264762010-04-02 22:27:05 +0000921 Op->StartLoc = S;
922 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000923 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000924 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000925
926 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
927 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
928 Op->MBOpt.Val = Opt;
929 Op->StartLoc = S;
930 Op->EndLoc = S;
931 return Op;
932 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000933
934 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
935 ARMOperand *Op = new ARMOperand(ProcIFlags);
936 Op->IFlags.Val = IFlags;
937 Op->StartLoc = S;
938 Op->EndLoc = S;
939 return Op;
940 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000941
942 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
943 ARMOperand *Op = new ARMOperand(MSRMask);
944 Op->MMask.Val = MMask;
945 Op->StartLoc = S;
946 Op->EndLoc = S;
947 return Op;
948 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000949};
950
951} // end anonymous namespace.
952
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000953void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000954 switch (Kind) {
955 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000956 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000957 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000958 case CCOut:
959 OS << "<ccout " << getReg() << ">";
960 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000961 case CoprocNum:
962 OS << "<coprocessor number: " << getCoproc() << ">";
963 break;
964 case CoprocReg:
965 OS << "<coprocessor register: " << getCoproc() << ">";
966 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000967 case MSRMask:
968 OS << "<mask: " << getMSRMask() << ">";
969 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000970 case Immediate:
971 getImm()->print(OS);
972 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000973 case MemBarrierOpt:
974 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
975 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000976 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000977 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000978 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
979 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000980 if (getMemOffsetIsReg()) {
981 OS << " offset:<register " << getMemOffsetRegNum();
982 if (getMemOffsetRegShifted()) {
983 OS << " offset-shift-type:" << getMemShiftType();
984 OS << " offset-shift-amount:" << *getMemShiftAmount();
985 }
986 } else {
987 OS << " offset:" << *getMemOffset();
988 }
989 if (getMemOffsetIsReg())
990 OS << " (offset-is-reg)";
991 if (getMemPreindexed())
992 OS << " (pre-indexed)";
993 if (getMemPostindexed())
994 OS << " (post-indexed)";
995 if (getMemNegative())
996 OS << " (negative)";
997 if (getMemWriteback())
998 OS << " (writeback)";
999 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001000 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001001 case ProcIFlags: {
1002 OS << "<ARM_PROC::";
1003 unsigned IFlags = getProcIFlags();
1004 for (int i=2; i >= 0; --i)
1005 if (IFlags & (1 << i))
1006 OS << ARM_PROC::IFlagsToString(1 << i);
1007 OS << ">";
1008 break;
1009 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001010 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +00001011 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001012 break;
Owen Anderson00828302011-03-18 22:50:18 +00001013 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +00001014 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
1015 break;
1016 case ShiftedRegister:
1017 OS << "<so_reg"
1018 << ShiftedReg.SrcReg
1019 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
1020 << ", " << ShiftedReg.ShiftReg << ", "
1021 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
1022 << ">";
Owen Anderson00828302011-03-18 22:50:18 +00001023 break;
Bill Wendling0f630752010-11-17 04:32:08 +00001024 case RegisterList:
1025 case DPRRegisterList:
1026 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00001027 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001028
Bill Wendling5fa22a12010-11-09 23:28:44 +00001029 const SmallVectorImpl<unsigned> &RegList = getRegList();
1030 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001031 I = RegList.begin(), E = RegList.end(); I != E; ) {
1032 OS << *I;
1033 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001034 }
1035
1036 OS << ">";
1037 break;
1038 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001039 case Token:
1040 OS << "'" << getToken() << "'";
1041 break;
1042 }
1043}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001044
1045/// @name Auto-generated Match Functions
1046/// {
1047
1048static unsigned MatchRegisterName(StringRef Name);
1049
1050/// }
1051
Bob Wilson69df7232011-02-03 21:46:10 +00001052bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1053 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +00001054 RegNo = TryParseRegister();
1055
1056 return (RegNo == (unsigned)-1);
1057}
1058
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001059/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00001060/// and if it is a register name the token is eaten and the register number is
1061/// returned. Otherwise return -1.
1062///
1063int ARMAsmParser::TryParseRegister() {
1064 const AsmToken &Tok = Parser.getTok();
1065 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001066
Chris Lattnere5658fa2010-10-30 04:09:10 +00001067 // FIXME: Validate register for the current architecture; we have to do
1068 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001069 std::string upperCase = Tok.getString().str();
1070 std::string lowerCase = LowercaseString(upperCase);
1071 unsigned RegNum = MatchRegisterName(lowerCase);
1072 if (!RegNum) {
1073 RegNum = StringSwitch<unsigned>(lowerCase)
1074 .Case("r13", ARM::SP)
1075 .Case("r14", ARM::LR)
1076 .Case("r15", ARM::PC)
1077 .Case("ip", ARM::R12)
1078 .Default(0);
1079 }
1080 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001081
Chris Lattnere5658fa2010-10-30 04:09:10 +00001082 Parser.Lex(); // Eat identifier token.
1083 return RegNum;
1084}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001085
Jim Grosbach19906722011-07-13 18:49:30 +00001086// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1087// If a recoverable error occurs, return 1. If an irrecoverable error
1088// occurs, return -1. An irrecoverable error is one where tokens have been
1089// consumed in the process of trying to parse the shifter (i.e., when it is
1090// indeed a shifter operand, but malformed).
1091int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001092 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1093 SMLoc S = Parser.getTok().getLoc();
1094 const AsmToken &Tok = Parser.getTok();
1095 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1096
1097 std::string upperCase = Tok.getString().str();
1098 std::string lowerCase = LowercaseString(upperCase);
1099 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1100 .Case("lsl", ARM_AM::lsl)
1101 .Case("lsr", ARM_AM::lsr)
1102 .Case("asr", ARM_AM::asr)
1103 .Case("ror", ARM_AM::ror)
1104 .Case("rrx", ARM_AM::rrx)
1105 .Default(ARM_AM::no_shift);
1106
1107 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001108 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001109
Jim Grosbache8606dc2011-07-13 17:50:29 +00001110 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001111
Jim Grosbache8606dc2011-07-13 17:50:29 +00001112 // The source register for the shift has already been added to the
1113 // operand list, so we need to pop it off and combine it into the shifted
1114 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00001115 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00001116 if (!PrevOp->isReg())
1117 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1118 int SrcReg = PrevOp->getReg();
1119 int64_t Imm = 0;
1120 int ShiftReg = 0;
1121 if (ShiftTy == ARM_AM::rrx) {
1122 // RRX Doesn't have an explicit shift amount. The encoder expects
1123 // the shift register to be the same as the source register. Seems odd,
1124 // but OK.
1125 ShiftReg = SrcReg;
1126 } else {
1127 // Figure out if this is shifted by a constant or a register (for non-RRX).
1128 if (Parser.getTok().is(AsmToken::Hash)) {
1129 Parser.Lex(); // Eat hash.
1130 SMLoc ImmLoc = Parser.getTok().getLoc();
1131 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001132 if (getParser().ParseExpression(ShiftExpr)) {
1133 Error(ImmLoc, "invalid immediate shift value");
1134 return -1;
1135 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001136 // The expression must be evaluatable as an immediate.
1137 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001138 if (!CE) {
1139 Error(ImmLoc, "invalid immediate shift value");
1140 return -1;
1141 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001142 // Range check the immediate.
1143 // lsl, ror: 0 <= imm <= 31
1144 // lsr, asr: 0 <= imm <= 32
1145 Imm = CE->getValue();
1146 if (Imm < 0 ||
1147 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1148 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001149 Error(ImmLoc, "immediate shift value out of range");
1150 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001151 }
1152 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1153 ShiftReg = TryParseRegister();
1154 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001155 if (ShiftReg == -1) {
1156 Error (L, "expected immediate or register in shift operand");
1157 return -1;
1158 }
1159 } else {
1160 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001161 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001162 return -1;
1163 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001164 }
1165
Jim Grosbache8606dc2011-07-13 17:50:29 +00001166 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1167 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001168 S, Parser.getTok().getLoc()));
1169
Jim Grosbach19906722011-07-13 18:49:30 +00001170 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001171}
1172
1173
Bill Wendling50d0f582010-11-18 23:43:05 +00001174/// Try to parse a register name. The token must be an Identifier when called.
1175/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1176/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001177///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001178/// TODO this is likely to change to allow different register types and or to
1179/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001180bool ARMAsmParser::
1181TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001182 SMLoc S = Parser.getTok().getLoc();
1183 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001184 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001185 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001186
Bill Wendling50d0f582010-11-18 23:43:05 +00001187 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001188
Chris Lattnere5658fa2010-10-30 04:09:10 +00001189 const AsmToken &ExclaimTok = Parser.getTok();
1190 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001191 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1192 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001193 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001194 }
1195
Bill Wendling50d0f582010-11-18 23:43:05 +00001196 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001197}
1198
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001199/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1200/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1201/// "c5", ...
1202static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001203 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1204 // but efficient.
1205 switch (Name.size()) {
1206 default: break;
1207 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001208 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001209 return -1;
1210 switch (Name[1]) {
1211 default: return -1;
1212 case '0': return 0;
1213 case '1': return 1;
1214 case '2': return 2;
1215 case '3': return 3;
1216 case '4': return 4;
1217 case '5': return 5;
1218 case '6': return 6;
1219 case '7': return 7;
1220 case '8': return 8;
1221 case '9': return 9;
1222 }
1223 break;
1224 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001225 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001226 return -1;
1227 switch (Name[2]) {
1228 default: return -1;
1229 case '0': return 10;
1230 case '1': return 11;
1231 case '2': return 12;
1232 case '3': return 13;
1233 case '4': return 14;
1234 case '5': return 15;
1235 }
1236 break;
1237 }
1238
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001239 return -1;
1240}
1241
Jim Grosbachf922c472011-02-12 01:34:40 +00001242/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001243/// token must be an Identifier when called, and if it is a coprocessor
1244/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001245ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1246tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001247 SMLoc S = Parser.getTok().getLoc();
1248 const AsmToken &Tok = Parser.getTok();
1249 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1250
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001251 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001252 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001253 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001254
1255 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001256 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001257 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001258}
1259
Jim Grosbachf922c472011-02-12 01:34:40 +00001260/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001261/// token must be an Identifier when called, and if it is a coprocessor
1262/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001263ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1264tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001265 SMLoc S = Parser.getTok().getLoc();
1266 const AsmToken &Tok = Parser.getTok();
1267 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1268
1269 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1270 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001271 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001272
1273 Parser.Lex(); // Eat identifier token.
1274 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001275 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001276}
1277
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001278/// Parse a register list, return it if successful else return null. The first
1279/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001280bool ARMAsmParser::
1281ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001282 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001283 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001284 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001285
Bill Wendling7729e062010-11-09 22:44:22 +00001286 // Read the rest of the registers in the list.
1287 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001288 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001289
Bill Wendling7729e062010-11-09 22:44:22 +00001290 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001291 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001292 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001293
Sean Callanan18b83232010-01-19 21:44:56 +00001294 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001295 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001296 if (RegTok.isNot(AsmToken::Identifier)) {
1297 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001298 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001299 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001300
Bill Wendling1d6a2652010-11-06 10:40:24 +00001301 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001302 if (RegNum == -1) {
1303 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001304 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001305 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001306
Bill Wendlinge7176102010-11-06 22:36:58 +00001307 if (IsRange) {
1308 int Reg = PrevRegNum;
1309 do {
1310 ++Reg;
1311 Registers.push_back(std::make_pair(Reg, RegLoc));
1312 } while (Reg != RegNum);
1313 } else {
1314 Registers.push_back(std::make_pair(RegNum, RegLoc));
1315 }
1316
1317 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001318 } while (Parser.getTok().is(AsmToken::Comma) ||
1319 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001320
1321 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001322 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001323 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1324 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001325 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001326 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001327
Bill Wendlinge7176102010-11-06 22:36:58 +00001328 SMLoc E = RCurlyTok.getLoc();
1329 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001330
Bill Wendlinge7176102010-11-06 22:36:58 +00001331 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001332 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001333 RI = Registers.begin(), RE = Registers.end();
1334
Bill Wendling7caebff2011-01-12 21:20:59 +00001335 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001336 bool EmittedWarning = false;
1337
Bill Wendling7caebff2011-01-12 21:20:59 +00001338 DenseMap<unsigned, bool> RegMap;
1339 RegMap[HighRegNum] = true;
1340
Bill Wendlinge7176102010-11-06 22:36:58 +00001341 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001342 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001343 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001344
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001345 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001346 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001347 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001348 }
1349
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001350 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001351 Warning(RegInfo.second,
1352 "register not in ascending order in register list");
1353
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001354 RegMap[Reg] = true;
1355 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001356 }
1357
Bill Wendling50d0f582010-11-18 23:43:05 +00001358 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1359 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001360}
1361
Jim Grosbachf922c472011-02-12 01:34:40 +00001362/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1363ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1364tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001365 SMLoc S = Parser.getTok().getLoc();
1366 const AsmToken &Tok = Parser.getTok();
1367 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1368 StringRef OptStr = Tok.getString();
1369
1370 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1371 .Case("sy", ARM_MB::SY)
1372 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001373 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001374 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001375 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001376 .Case("ishst", ARM_MB::ISHST)
1377 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001378 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001379 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001380 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001381 .Case("osh", ARM_MB::OSH)
1382 .Case("oshst", ARM_MB::OSHST)
1383 .Default(~0U);
1384
1385 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001386 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001387
1388 Parser.Lex(); // Eat identifier token.
1389 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001390 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001391}
1392
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001393/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001394ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1395tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1396 SMLoc S = Parser.getTok().getLoc();
1397 const AsmToken &Tok = Parser.getTok();
1398 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1399 StringRef IFlagsStr = Tok.getString();
1400
1401 unsigned IFlags = 0;
1402 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1403 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1404 .Case("a", ARM_PROC::A)
1405 .Case("i", ARM_PROC::I)
1406 .Case("f", ARM_PROC::F)
1407 .Default(~0U);
1408
1409 // If some specific iflag is already set, it means that some letter is
1410 // present more than once, this is not acceptable.
1411 if (Flag == ~0U || (IFlags & Flag))
1412 return MatchOperand_NoMatch;
1413
1414 IFlags |= Flag;
1415 }
1416
1417 Parser.Lex(); // Eat identifier token.
1418 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1419 return MatchOperand_Success;
1420}
1421
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001422/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1423ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1424tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1425 SMLoc S = Parser.getTok().getLoc();
1426 const AsmToken &Tok = Parser.getTok();
1427 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1428 StringRef Mask = Tok.getString();
1429
1430 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1431 size_t Start = 0, Next = Mask.find('_');
1432 StringRef Flags = "";
1433 StringRef SpecReg = Mask.slice(Start, Next);
1434 if (Next != StringRef::npos)
1435 Flags = Mask.slice(Next+1, Mask.size());
1436
1437 // FlagsVal contains the complete mask:
1438 // 3-0: Mask
1439 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1440 unsigned FlagsVal = 0;
1441
1442 if (SpecReg == "apsr") {
1443 FlagsVal = StringSwitch<unsigned>(Flags)
1444 .Case("nzcvq", 0x8) // same as CPSR_c
1445 .Case("g", 0x4) // same as CPSR_s
1446 .Case("nzcvqg", 0xc) // same as CPSR_fs
1447 .Default(~0U);
1448
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001449 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001450 if (!Flags.empty())
1451 return MatchOperand_NoMatch;
1452 else
1453 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001454 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001455 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001456 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1457 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001458 for (int i = 0, e = Flags.size(); i != e; ++i) {
1459 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1460 .Case("c", 1)
1461 .Case("x", 2)
1462 .Case("s", 4)
1463 .Case("f", 8)
1464 .Default(~0U);
1465
1466 // If some specific flag is already set, it means that some letter is
1467 // present more than once, this is not acceptable.
1468 if (FlagsVal == ~0U || (FlagsVal & Flag))
1469 return MatchOperand_NoMatch;
1470 FlagsVal |= Flag;
1471 }
1472 } else // No match for special register.
1473 return MatchOperand_NoMatch;
1474
1475 // Special register without flags are equivalent to "fc" flags.
1476 if (!FlagsVal)
1477 FlagsVal = 0x9;
1478
1479 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1480 if (SpecReg == "spsr")
1481 FlagsVal |= 16;
1482
1483 Parser.Lex(); // Eat identifier token.
1484 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1485 return MatchOperand_Success;
1486}
1487
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001488/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1489ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1490tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001491 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001492
1493 if (ParseMemory(Operands, ARMII::AddrMode2))
1494 return MatchOperand_NoMatch;
1495
1496 return MatchOperand_Success;
1497}
1498
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001499/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1500ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1501tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1502 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1503
1504 if (ParseMemory(Operands, ARMII::AddrMode3))
1505 return MatchOperand_NoMatch;
1506
1507 return MatchOperand_Success;
1508}
1509
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001510/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1511/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1512/// when they refer multiple MIOperands inside a single one.
1513bool ARMAsmParser::
1514CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1515 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1516 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1517
1518 // Create a writeback register dummy placeholder.
1519 Inst.addOperand(MCOperand::CreateImm(0));
1520
1521 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1522 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1523 return true;
1524}
1525
1526/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1527/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1528/// when they refer multiple MIOperands inside a single one.
1529bool ARMAsmParser::
1530CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1531 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1532 // Create a writeback register dummy placeholder.
1533 Inst.addOperand(MCOperand::CreateImm(0));
1534 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1535 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1536 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1537 return true;
1538}
1539
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001540/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1541/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1542/// when they refer multiple MIOperands inside a single one.
1543bool ARMAsmParser::
1544CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1545 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1546 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1547
1548 // Create a writeback register dummy placeholder.
1549 Inst.addOperand(MCOperand::CreateImm(0));
1550
1551 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1552 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1553 return true;
1554}
1555
1556/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1557/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1558/// when they refer multiple MIOperands inside a single one.
1559bool ARMAsmParser::
1560CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1561 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1562 // Create a writeback register dummy placeholder.
1563 Inst.addOperand(MCOperand::CreateImm(0));
1564 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1565 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1566 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1567 return true;
1568}
1569
Bill Wendlinge7176102010-11-06 22:36:58 +00001570/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001571/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001572///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001573/// TODO Only preindexing and postindexing addressing are started, unindexed
1574/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001575bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001576ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1577 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001578 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001579 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001580 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001581 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001582 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001583
Sean Callanan18b83232010-01-19 21:44:56 +00001584 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001585 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1586 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001587 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001588 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001589 int BaseRegNum = TryParseRegister();
1590 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001591 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001592 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001593 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001594
Daniel Dunbar05710932011-01-18 05:34:17 +00001595 // The next token must either be a comma or a closing bracket.
1596 const AsmToken &Tok = Parser.getTok();
1597 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1598 return true;
1599
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001600 bool Preindexed = false;
1601 bool Postindexed = false;
1602 bool OffsetIsReg = false;
1603 bool Negative = false;
1604 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001605 ARMOperand *WBOp = 0;
1606 int OffsetRegNum = -1;
1607 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001608 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001609 const MCExpr *ShiftAmount = 0;
1610 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001611
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001612 // First look for preindexed address forms, that is after the "[Rn" we now
1613 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001614 if (Tok.is(AsmToken::Comma)) {
1615 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001616 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001617
Chris Lattner550276e2010-10-28 20:52:15 +00001618 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1619 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001620 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001621 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001622 if (RBracTok.isNot(AsmToken::RBrac)) {
1623 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001624 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001625 }
Sean Callanan76264762010-04-02 22:27:05 +00001626 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001627 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001628
Sean Callanan18b83232010-01-19 21:44:56 +00001629 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001630 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001631 // None of addrmode3 instruction uses "!"
1632 if (AddrMode == ARMII::AddrMode3)
1633 return true;
1634
Bill Wendling50d0f582010-11-18 23:43:05 +00001635 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1636 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001637 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001638 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001639 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1640 if (AddrMode == ARMII::AddrMode2)
1641 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001642 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001643 } else {
1644 // The "[Rn" we have so far was not followed by a comma.
1645
Jim Grosbach80eb2332010-10-29 17:41:25 +00001646 // If there's anything other than the right brace, this is a post indexing
1647 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001648 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001649 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001650
Sean Callanan18b83232010-01-19 21:44:56 +00001651 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001652
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001653 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001654 Postindexed = true;
1655 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001656
Chris Lattner550276e2010-10-28 20:52:15 +00001657 if (NextTok.isNot(AsmToken::Comma)) {
1658 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001659 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001660 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001661
Sean Callananb9a25b72010-01-19 20:27:46 +00001662 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001663
Chris Lattner550276e2010-10-28 20:52:15 +00001664 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001665 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001666 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001667 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001668 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001669 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001670
1671 // Force Offset to exist if used.
1672 if (!OffsetIsReg) {
1673 if (!Offset)
1674 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001675 } else {
1676 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1677 Error(E, "shift amount not supported");
1678 return true;
1679 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001680 }
1681
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001682 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1683 Offset, OffsetRegNum, OffsetRegShifted,
1684 ShiftType, ShiftAmount, Preindexed,
1685 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001686 if (WBOp)
1687 Operands.push_back(WBOp);
1688
1689 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001690}
1691
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001692/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1693/// we will parse the following (were +/- means that a plus or minus is
1694/// optional):
1695/// +/-Rm
1696/// +/-Rm, shift
1697/// #offset
1698/// we return false on success or an error otherwise.
1699bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001700 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001701 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001702 const MCExpr *&ShiftAmount,
1703 const MCExpr *&Offset,
1704 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001705 int &OffsetRegNum,
1706 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001707 Negative = false;
1708 OffsetRegShifted = false;
1709 OffsetIsReg = false;
1710 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001711 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001712 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001713 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001714 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001715 else if (NextTok.is(AsmToken::Minus)) {
1716 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001717 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001718 }
1719 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001720 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001721 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001722 SMLoc CurLoc = OffsetRegTok.getLoc();
1723 OffsetRegNum = TryParseRegister();
1724 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001725 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001726 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001727 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001728 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001729
Bill Wendling12f40e92010-11-06 10:51:53 +00001730 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001731 if (OffsetRegNum != -1) {
1732 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001733 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001734 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001735 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001736
Sean Callanan18b83232010-01-19 21:44:56 +00001737 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001738 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001739 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001740 OffsetRegShifted = true;
1741 }
1742 }
1743 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1744 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001745 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001746 if (HashTok.isNot(AsmToken::Hash))
1747 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001748
Sean Callananb9a25b72010-01-19 20:27:46 +00001749 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001750
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001751 if (getParser().ParseExpression(Offset))
1752 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001753 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001754 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001755 return false;
1756}
1757
1758/// ParseShift as one of these two:
1759/// ( lsl | lsr | asr | ror ) , # shift_amount
1760/// rrx
1761/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001762bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1763 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001764 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001765 if (Tok.isNot(AsmToken::Identifier))
1766 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001767 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001768 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001769 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001770 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001771 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001772 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001773 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001774 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001775 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001776 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001777 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001778 else
1779 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001780 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001781
1782 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001783 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001784 return false;
1785
1786 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001787 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001788 if (HashTok.isNot(AsmToken::Hash))
1789 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001790 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001791
1792 if (getParser().ParseExpression(ShiftAmount))
1793 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001794
1795 return false;
1796}
1797
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001798/// Parse a arm instruction operand. For now this parses the operand regardless
1799/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001800bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001801 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001802 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001803
1804 // Check if the current operand has a custom associated parser, if so, try to
1805 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001806 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1807 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001808 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001809 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1810 // there was a match, but an error occurred, in which case, just return that
1811 // the operand parsing failed.
1812 if (ResTy == MatchOperand_ParseFail)
1813 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001814
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001815 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001816 default:
1817 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001818 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001819 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001820 if (!TryParseRegisterWithWriteBack(Operands))
1821 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001822 int Res = TryParseShiftRegister(Operands);
1823 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001824 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001825 else if (Res == -1) // irrecoverable error
1826 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001827
1828 // Fall though for the Identifier case that is not a register or a
1829 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00001830 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00001831 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1832 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001833 // This was not a register so parse other operands that start with an
1834 // identifier (like labels) as expressions and create them as immediates.
1835 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001836 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001837 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001838 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001839 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001840 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1841 return false;
1842 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001843 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001844 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001845 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001846 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001847 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001848 // #42 -> immediate.
1849 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001850 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001851 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001852 const MCExpr *ImmVal;
1853 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001854 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001855 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001856 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1857 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001858 case AsmToken::Colon: {
1859 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001860 // FIXME: Check it's an expression prefix,
1861 // e.g. (FOO - :lower16:BAR) isn't legal.
1862 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001863 if (ParsePrefix(RefKind))
1864 return true;
1865
Evan Cheng75972122011-01-13 07:58:56 +00001866 const MCExpr *SubExprVal;
1867 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001868 return true;
1869
Evan Cheng75972122011-01-13 07:58:56 +00001870 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1871 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001872 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001873 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001874 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001875 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001876 }
1877}
1878
Evan Cheng75972122011-01-13 07:58:56 +00001879// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1880// :lower16: and :upper16:.
1881bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1882 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001883
1884 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001885 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001886 Parser.Lex(); // Eat ':'
1887
1888 if (getLexer().isNot(AsmToken::Identifier)) {
1889 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1890 return true;
1891 }
1892
1893 StringRef IDVal = Parser.getTok().getIdentifier();
1894 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001895 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001896 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001897 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001898 } else {
1899 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1900 return true;
1901 }
1902 Parser.Lex();
1903
1904 if (getLexer().isNot(AsmToken::Colon)) {
1905 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1906 return true;
1907 }
1908 Parser.Lex(); // Eat the last ':'
1909 return false;
1910}
1911
1912const MCExpr *
1913ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1914 MCSymbolRefExpr::VariantKind Variant) {
1915 // Recurse over the given expression, rebuilding it to apply the given variant
1916 // to the leftmost symbol.
1917 if (Variant == MCSymbolRefExpr::VK_None)
1918 return E;
1919
1920 switch (E->getKind()) {
1921 case MCExpr::Target:
1922 llvm_unreachable("Can't handle target expr yet");
1923 case MCExpr::Constant:
1924 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1925
1926 case MCExpr::SymbolRef: {
1927 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1928
1929 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1930 return 0;
1931
1932 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1933 }
1934
1935 case MCExpr::Unary:
1936 llvm_unreachable("Can't handle unary expressions yet");
1937
1938 case MCExpr::Binary: {
1939 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1940 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1941 const MCExpr *RHS = BE->getRHS();
1942 if (!LHS)
1943 return 0;
1944
1945 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1946 }
1947 }
1948
1949 assert(0 && "Invalid expression kind!");
1950 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001951}
1952
Daniel Dunbar352e1482011-01-11 15:59:50 +00001953/// \brief Given a mnemonic, split out possible predication code and carry
1954/// setting letters to form a canonical mnemonic and flags.
1955//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001956// FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00001957StringRef ARMAsmParser::SplitMnemonic(StringRef Mnemonic,
1958 unsigned &PredicationCode,
1959 bool &CarrySetting,
1960 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001961 PredicationCode = ARMCC::AL;
1962 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001963 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001964
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001965 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001966 //
1967 // FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00001968 if ((Mnemonic == "movs" && isThumb()) ||
1969 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
1970 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1971 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
1972 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
1973 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
1974 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
1975 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
Daniel Dunbar352e1482011-01-11 15:59:50 +00001976 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001977
Jim Grosbach3f00e312011-07-11 17:09:57 +00001978 // First, split out any predication code. Ignore mnemonics we know aren't
1979 // predicated but do have a carry-set and so weren't caught above.
Jim Grosbach5f160572011-07-19 20:10:31 +00001980 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs") {
Jim Grosbach3f00e312011-07-11 17:09:57 +00001981 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1982 .Case("eq", ARMCC::EQ)
1983 .Case("ne", ARMCC::NE)
1984 .Case("hs", ARMCC::HS)
1985 .Case("cs", ARMCC::HS)
1986 .Case("lo", ARMCC::LO)
1987 .Case("cc", ARMCC::LO)
1988 .Case("mi", ARMCC::MI)
1989 .Case("pl", ARMCC::PL)
1990 .Case("vs", ARMCC::VS)
1991 .Case("vc", ARMCC::VC)
1992 .Case("hi", ARMCC::HI)
1993 .Case("ls", ARMCC::LS)
1994 .Case("ge", ARMCC::GE)
1995 .Case("lt", ARMCC::LT)
1996 .Case("gt", ARMCC::GT)
1997 .Case("le", ARMCC::LE)
1998 .Case("al", ARMCC::AL)
1999 .Default(~0U);
2000 if (CC != ~0U) {
2001 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
2002 PredicationCode = CC;
2003 }
Bill Wendling52925b62010-10-29 23:50:21 +00002004 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002005
Daniel Dunbar352e1482011-01-11 15:59:50 +00002006 // Next, determine if we have a carry setting bit. We explicitly ignore all
2007 // the instructions we know end in 's'.
2008 if (Mnemonic.endswith("s") &&
2009 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002010 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
2011 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
2012 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
2013 Mnemonic == "vrsqrts" || (Mnemonic == "movs" && isThumb()))) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00002014 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
2015 CarrySetting = true;
2016 }
2017
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002018 // The "cps" instruction can have a interrupt mode operand which is glued into
2019 // the mnemonic. Check if this is the case, split it and parse the imod op
2020 if (Mnemonic.startswith("cps")) {
2021 // Split out any imod code.
2022 unsigned IMod =
2023 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
2024 .Case("ie", ARM_PROC::IE)
2025 .Case("id", ARM_PROC::ID)
2026 .Default(~0U);
2027 if (IMod != ~0U) {
2028 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2029 ProcessorIMod = IMod;
2030 }
2031 }
2032
Daniel Dunbar352e1482011-01-11 15:59:50 +00002033 return Mnemonic;
2034}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002035
2036/// \brief Given a canonical mnemonic, determine if the instruction ever allows
2037/// inclusion of carry set or predication code operands.
2038//
2039// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00002040void ARMAsmParser::
2041GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2042 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002043 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2044 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2045 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2046 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002047 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002048 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2049 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002050 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00002051 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002052 CanAcceptCarrySet = true;
2053 } else {
2054 CanAcceptCarrySet = false;
2055 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002056
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002057 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2058 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2059 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2060 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00002061 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "clrex" ||
2062 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumb())) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002063 CanAcceptPredicationCode = false;
2064 } else {
2065 CanAcceptPredicationCode = true;
2066 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002067
Evan Chengebdeeab2011-07-08 01:53:10 +00002068 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002069 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002070 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002071 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002072}
2073
2074/// Parse an arm instruction mnemonic followed by its operands.
2075bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2076 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2077 // Create the leading tokens for the mnemonic, split by '.' characters.
2078 size_t Start = 0, Next = Name.find('.');
Jim Grosbachffa32252011-07-19 19:13:28 +00002079 StringRef Mnemonic = Name.slice(Start, Next);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002080
Daniel Dunbar352e1482011-01-11 15:59:50 +00002081 // Split out the predication code and carry setting flag from the mnemonic.
2082 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002083 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002084 bool CarrySetting;
Jim Grosbachffa32252011-07-19 19:13:28 +00002085 Mnemonic = SplitMnemonic(Mnemonic, PredicationCode, CarrySetting,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002086 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002087
Jim Grosbachffa32252011-07-19 19:13:28 +00002088 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
2089
2090 // FIXME: This is all a pretty gross hack. We should automatically handle
2091 // optional operands like this via tblgen.
Bill Wendling9717fa92010-11-21 10:56:05 +00002092
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002093 // Next, add the CCOut and ConditionCode operands, if needed.
2094 //
2095 // For mnemonics which can ever incorporate a carry setting bit or predication
2096 // code, our matching model involves us always generating CCOut and
2097 // ConditionCode operands to match the mnemonic "as written" and then we let
2098 // the matcher deal with finding the right instruction or generating an
2099 // appropriate error.
2100 bool CanAcceptCarrySet, CanAcceptPredicationCode;
Jim Grosbachffa32252011-07-19 19:13:28 +00002101 GetMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002102
Jim Grosbach33c16a22011-07-14 22:04:21 +00002103 // If we had a carry-set on an instruction that can't do that, issue an
2104 // error.
2105 if (!CanAcceptCarrySet && CarrySetting) {
2106 Parser.EatToEndOfStatement();
Jim Grosbachffa32252011-07-19 19:13:28 +00002107 return Error(NameLoc, "instruction '" + Mnemonic +
Jim Grosbach33c16a22011-07-14 22:04:21 +00002108 "' can not set flags, but 's' suffix specified");
2109 }
2110
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002111 // Add the carry setting operand, if necessary.
2112 //
2113 // FIXME: It would be awesome if we could somehow invent a location such that
2114 // match errors on this operand would print a nice diagnostic about how the
2115 // 's' character in the mnemonic resulted in a CCOut operand.
Jim Grosbach33c16a22011-07-14 22:04:21 +00002116 if (CanAcceptCarrySet)
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002117 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2118 NameLoc));
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002119
2120 // Add the predication code operand, if necessary.
2121 if (CanAcceptPredicationCode) {
2122 Operands.push_back(ARMOperand::CreateCondCode(
2123 ARMCC::CondCodes(PredicationCode), NameLoc));
2124 } else {
2125 // This mnemonic can't ever accept a predication code, but the user wrote
2126 // one (or misspelled another mnemonic).
2127
2128 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002129 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002130
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002131 // Add the processor imod operand, if necessary.
2132 if (ProcessorIMod) {
2133 Operands.push_back(ARMOperand::CreateImm(
2134 MCConstantExpr::Create(ProcessorIMod, getContext()),
2135 NameLoc, NameLoc));
2136 } else {
2137 // This mnemonic can't ever accept a imod, but the user wrote
2138 // one (or misspelled another mnemonic).
2139
2140 // FIXME: Issue a nice error.
2141 }
2142
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002143 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002144 while (Next != StringRef::npos) {
2145 Start = Next;
2146 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002147 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002148
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002149 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002150 }
2151
2152 // Read the remaining operands.
2153 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002154 // Read the first operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002155 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002156 Parser.EatToEndOfStatement();
2157 return true;
2158 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002159
2160 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002161 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002162
2163 // Parse and remember the operand.
Jim Grosbachffa32252011-07-19 19:13:28 +00002164 if (ParseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002165 Parser.EatToEndOfStatement();
2166 return true;
2167 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002168 }
2169 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002170
Chris Lattnercbf8a982010-09-11 16:18:25 +00002171 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2172 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002173 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002174 }
Bill Wendling146018f2010-11-06 21:42:12 +00002175
Chris Lattner34e53142010-09-08 05:10:46 +00002176 Parser.Lex(); // Consume the EndOfStatement
Jim Grosbachffa32252011-07-19 19:13:28 +00002177
2178
2179 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
2180 // another does not. Specifically, the MOVW instruction does not. So we
2181 // special case it here and remove the defaulted (non-setting) cc_out
2182 // operand if that's the instruction we're trying to match.
2183 //
2184 // We do this post-processing of the explicit operands rather than just
2185 // conditionally adding the cc_out in the first place because we need
2186 // to check the type of the parsed immediate operand.
2187 if (Mnemonic == "mov" && Operands.size() > 4 &&
2188 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
Jim Grosbach731f2092011-07-19 19:45:44 +00002189 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
2190 static_cast<ARMOperand*>(Operands[1])->getReg() == 0) {
Jim Grosbachffa32252011-07-19 19:13:28 +00002191 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
2192 Operands.erase(Operands.begin() + 1);
2193 delete Op;
2194 }
2195
2196
2197
Chris Lattner98986712010-01-14 22:21:20 +00002198 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002199}
2200
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002201bool ARMAsmParser::
2202MatchAndEmitInstruction(SMLoc IDLoc,
2203 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2204 MCStreamer &Out) {
2205 MCInst Inst;
2206 unsigned ErrorInfo;
Jim Grosbach5a187002011-07-19 18:32:48 +00002207 MatchResultTy MatchResult;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002208 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002209 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002210 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002211 Out.EmitInstruction(Inst);
2212 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002213 case Match_MissingFeature:
2214 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2215 return true;
2216 case Match_InvalidOperand: {
2217 SMLoc ErrorLoc = IDLoc;
2218 if (ErrorInfo != ~0U) {
2219 if (ErrorInfo >= Operands.size())
2220 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002221
Chris Lattnere73d4f82010-10-28 21:41:58 +00002222 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2223 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2224 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002225
Chris Lattnere73d4f82010-10-28 21:41:58 +00002226 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002227 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002228 case Match_MnemonicFail:
2229 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002230 case Match_ConversionFail:
2231 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002232 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002233
Eric Christopherc223e2b2010-10-29 09:26:59 +00002234 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002235 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002236}
2237
Kevin Enderby515d5092009-10-15 20:48:48 +00002238/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002239bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2240 StringRef IDVal = DirectiveID.getIdentifier();
2241 if (IDVal == ".word")
2242 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002243 else if (IDVal == ".thumb")
2244 return ParseDirectiveThumb(DirectiveID.getLoc());
2245 else if (IDVal == ".thumb_func")
2246 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2247 else if (IDVal == ".code")
2248 return ParseDirectiveCode(DirectiveID.getLoc());
2249 else if (IDVal == ".syntax")
2250 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002251 return true;
2252}
2253
2254/// ParseDirectiveWord
2255/// ::= .word [ expression (, expression)* ]
2256bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2257 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2258 for (;;) {
2259 const MCExpr *Value;
2260 if (getParser().ParseExpression(Value))
2261 return true;
2262
Chris Lattneraaec2052010-01-19 19:46:13 +00002263 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002264
2265 if (getLexer().is(AsmToken::EndOfStatement))
2266 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002267
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002268 // FIXME: Improve diagnostic.
2269 if (getLexer().isNot(AsmToken::Comma))
2270 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002271 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002272 }
2273 }
2274
Sean Callananb9a25b72010-01-19 20:27:46 +00002275 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002276 return false;
2277}
2278
Kevin Enderby515d5092009-10-15 20:48:48 +00002279/// ParseDirectiveThumb
2280/// ::= .thumb
2281bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2282 if (getLexer().isNot(AsmToken::EndOfStatement))
2283 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002284 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002285
2286 // TODO: set thumb mode
2287 // TODO: tell the MC streamer the mode
2288 // getParser().getStreamer().Emit???();
2289 return false;
2290}
2291
2292/// ParseDirectiveThumbFunc
2293/// ::= .thumbfunc symbol_name
2294bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002295 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2296 bool isMachO = MAI.hasSubsectionsViaSymbols();
2297 StringRef Name;
2298
2299 // Darwin asm has function name after .thumb_func direction
2300 // ELF doesn't
2301 if (isMachO) {
2302 const AsmToken &Tok = Parser.getTok();
2303 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2304 return Error(L, "unexpected token in .thumb_func directive");
2305 Name = Tok.getString();
2306 Parser.Lex(); // Consume the identifier token.
2307 }
2308
Kevin Enderby515d5092009-10-15 20:48:48 +00002309 if (getLexer().isNot(AsmToken::EndOfStatement))
2310 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002311 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002312
Rafael Espindola64695402011-05-16 16:17:21 +00002313 // FIXME: assuming function name will be the line following .thumb_func
2314 if (!isMachO) {
2315 Name = Parser.getTok().getString();
2316 }
2317
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002318 // Mark symbol as a thumb symbol.
2319 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2320 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002321 return false;
2322}
2323
2324/// ParseDirectiveSyntax
2325/// ::= .syntax unified | divided
2326bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002327 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002328 if (Tok.isNot(AsmToken::Identifier))
2329 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002330 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002331 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002332 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002333 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002334 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002335 else
2336 return Error(L, "unrecognized syntax mode in .syntax directive");
2337
2338 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002339 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002340 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002341
2342 // TODO tell the MC streamer the mode
2343 // getParser().getStreamer().Emit???();
2344 return false;
2345}
2346
2347/// ParseDirectiveCode
2348/// ::= .code 16 | 32
2349bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002350 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002351 if (Tok.isNot(AsmToken::Integer))
2352 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002353 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002354 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002355 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002356 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002357 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002358 else
2359 return Error(L, "invalid operand to .code directive");
2360
2361 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002362 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002363 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002364
Evan Cheng32869202011-07-08 22:36:29 +00002365 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002366 if (!isThumb())
2367 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002368 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002369 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002370 if (isThumb())
2371 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002372 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002373 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002374
Kevin Enderby515d5092009-10-15 20:48:48 +00002375 return false;
2376}
2377
Sean Callanan90b70972010-04-07 20:29:34 +00002378extern "C" void LLVMInitializeARMAsmLexer();
2379
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002380/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002381extern "C" void LLVMInitializeARMAsmParser() {
2382 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2383 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002384 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002385}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002386
Chris Lattner0692ee62010-09-06 19:11:01 +00002387#define GET_REGISTER_MATCHER
2388#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002389#include "ARMGenAsmMatcher.inc"