blob: f927abbedaad604fad654486c00fe99ab9c424db [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ARM.h"
Bill Wendling92b5a2e2010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Evan Cheng75972122011-01-13 07:58:56 +000012#include "ARMMCExpr.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000013#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000014#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
17#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Rafael Espindola64695402011-05-16 16:17:21 +000018#include "llvm/MC/MCAsmInfo.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000019#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000020#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000023#include "llvm/MC/MCSubtargetInfo.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000024#include "llvm/Target/TargetRegistry.h"
25#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000027#include "llvm/Support/raw_ostream.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000028#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000030#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000031#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000032#include "llvm/ADT/Twine.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000033
34#define GET_SUBTARGETINFO_ENUM
35#include "ARMGenSubtargetInfo.inc"
36
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000037using namespace llvm;
38
Chris Lattner3a697562010-10-28 17:20:03 +000039namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000040
41class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000042
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000043class ARMAsmParser : public TargetAsmParser {
44 MCAsmParser &Parser;
Evan Cheng32869202011-07-08 22:36:29 +000045 /// STI, ARM_STI, Thumb_STI - Subtarget info for ARM and Thumb modes. STI
46 /// points to either ARM_STI or Thumb_STI depending on the mode.
47 const MCSubtargetInfo *STI;
48 OwningPtr<const MCSubtargetInfo> ARM_STI;
49 OwningPtr<const MCSubtargetInfo> Thumb_STI;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000050
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000051 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000052 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
53
54 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000055 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
56
Chris Lattnere5658fa2010-10-30 04:09:10 +000057 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000058 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000059 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Anderson00828302011-03-18 22:50:18 +000060 bool TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000061 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000062 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
63 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000064 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000065 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000066 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
67 MCSymbolRefExpr::VariantKind Variant);
68
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000069
Kevin Enderby9c41fa82009-10-30 22:55:57 +000070 bool ParseMemoryOffsetReg(bool &Negative,
71 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000072 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000073 const MCExpr *&ShiftAmount,
74 const MCExpr *&Offset,
75 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000076 int &OffsetRegNum,
77 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000078 bool ParseShift(enum ARM_AM::ShiftOpc &St,
79 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000080 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000081 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000082 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000083 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000084 bool ParseDirectiveSyntax(SMLoc L);
85
Chris Lattner7036f8b2010-09-29 01:42:58 +000086 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000087 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000088 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000089 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
90 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000091
Evan Chengebdeeab2011-07-08 01:53:10 +000092 bool isThumb() const {
93 // FIXME: Can tablegen auto-generate this?
94 return (STI->getFeatureBits() & ARM::ModeThumb) != 0;
95 }
Evan Chengebdeeab2011-07-08 01:53:10 +000096 bool isThumbOne() const {
97 return isThumb() && (STI->getFeatureBits() & ARM::FeatureThumb2) == 0;
98 }
Evan Cheng32869202011-07-08 22:36:29 +000099 void SwitchMode() {
100 STI = isThumb() ? ARM_STI.get() : Thumb_STI.get();
101 setAvailableFeatures(ComputeAvailableFeatures(STI->getFeatureBits()));
102 }
Evan Chengebdeeab2011-07-08 01:53:10 +0000103
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000104 /// @name Auto-generated Match Functions
105 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000106
Chris Lattner0692ee62010-09-06 19:11:01 +0000107#define GET_ASSEMBLER_HEADER
108#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000109
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000110 /// }
111
Jim Grosbachf922c472011-02-12 01:34:40 +0000112 OperandMatchResultTy tryParseCoprocNumOperand(
113 SmallVectorImpl<MCParsedAsmOperand*>&);
114 OperandMatchResultTy tryParseCoprocRegOperand(
115 SmallVectorImpl<MCParsedAsmOperand*>&);
116 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000117 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000118 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000119 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000120 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000121 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000122 OperandMatchResultTy tryParseMemMode2Operand(
123 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000124 OperandMatchResultTy tryParseMemMode3Operand(
125 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000126
127 // Asm Match Converter Methods
128 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
129 const SmallVectorImpl<MCParsedAsmOperand*> &);
130 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
131 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000132 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
133 const SmallVectorImpl<MCParsedAsmOperand*> &);
134 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
135 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000136
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000137public:
Evan Cheng480cee52011-07-08 19:33:14 +0000138 ARMAsmParser(StringRef TT, StringRef CPU, StringRef FS, MCAsmParser &_Parser)
Evan Cheng32869202011-07-08 22:36:29 +0000139 : TargetAsmParser(), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000140 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000141
142 STI = ARM_MC::createARMMCSubtargetInfo(TT, CPU, FS);
143 // FIXME: Design a better way to create two subtargets with only difference
144 // being a feature change.
145 if (isThumb()) {
146 Thumb_STI.reset(STI);
147 assert(TT.startswith("thumb") && "Unexpected Triple string for Thumb!");
148 Twine ARM_TT = "arm" + TT.substr(5);
149 ARM_STI.reset(ARM_MC::createARMMCSubtargetInfo(ARM_TT.str(), CPU, FS));
150 } else {
151 ARM_STI.reset(STI);
152 assert(TT.startswith("arm") && "Unexpected Triple string for ARM!");
153 Twine Thumb_TT = "thumb" + TT.substr(3);
154 Thumb_STI.reset(ARM_MC::createARMMCSubtargetInfo(Thumb_TT.str(),CPU, FS));
155 }
156
Evan Chengebdeeab2011-07-08 01:53:10 +0000157 // Initialize the set of available features.
158 setAvailableFeatures(ComputeAvailableFeatures(STI->getFeatureBits()));
159 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000160
Benjamin Kramer38e59892010-07-14 22:38:02 +0000161 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000162 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000163 virtual bool ParseDirective(AsmToken DirectiveID);
164};
Jim Grosbach16c74252010-10-29 14:46:02 +0000165} // end anonymous namespace
166
Chris Lattner3a697562010-10-28 17:20:03 +0000167namespace {
168
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000169/// ARMOperand - Instances of this class represent a parsed ARM machine
170/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000171class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000172 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000173 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000174 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000175 CoprocNum,
176 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000177 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000178 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000179 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000180 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000181 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000182 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000183 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000184 DPRRegisterList,
185 SPRRegisterList,
Owen Anderson00828302011-03-18 22:50:18 +0000186 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000187 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000188 } Kind;
189
Sean Callanan76264762010-04-02 22:27:05 +0000190 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000191 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000192
193 union {
194 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000195 ARMCC::CondCodes Val;
196 } CC;
197
198 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000199 ARM_MB::MemBOpt Val;
200 } MBOpt;
201
202 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000203 unsigned Val;
204 } Cop;
205
206 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000207 ARM_PROC::IFlags Val;
208 } IFlags;
209
210 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000211 unsigned Val;
212 } MMask;
213
214 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000215 const char *Data;
216 unsigned Length;
217 } Tok;
218
219 struct {
220 unsigned RegNum;
221 } Reg;
222
Bill Wendling8155e5b2010-11-06 22:19:43 +0000223 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000224 const MCExpr *Val;
225 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000226
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000227 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000228 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000229 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000230 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000231 union {
232 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
233 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
234 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000235 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000236 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000237 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000238 unsigned Preindexed : 1;
239 unsigned Postindexed : 1;
240 unsigned OffsetIsReg : 1;
241 unsigned Negative : 1; // only used when OffsetIsReg is true
242 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000243 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000244
245 struct {
246 ARM_AM::ShiftOpc ShiftTy;
247 unsigned RegNum;
248 } Shift;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000249 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000250
Bill Wendling146018f2010-11-06 21:42:12 +0000251 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
252public:
Sean Callanan76264762010-04-02 22:27:05 +0000253 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
254 Kind = o.Kind;
255 StartLoc = o.StartLoc;
256 EndLoc = o.EndLoc;
257 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000258 case CondCode:
259 CC = o.CC;
260 break;
Sean Callanan76264762010-04-02 22:27:05 +0000261 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000262 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000263 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000264 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000265 case Register:
266 Reg = o.Reg;
267 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000268 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000269 case DPRRegisterList:
270 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000271 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000272 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000273 case CoprocNum:
274 case CoprocReg:
275 Cop = o.Cop;
276 break;
Sean Callanan76264762010-04-02 22:27:05 +0000277 case Immediate:
278 Imm = o.Imm;
279 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000280 case MemBarrierOpt:
281 MBOpt = o.MBOpt;
282 break;
Sean Callanan76264762010-04-02 22:27:05 +0000283 case Memory:
284 Mem = o.Mem;
285 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000286 case MSRMask:
287 MMask = o.MMask;
288 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000289 case ProcIFlags:
290 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000291 break;
292 case Shifter:
293 Shift = o.Shift;
294 break;
Sean Callanan76264762010-04-02 22:27:05 +0000295 }
296 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000297
Sean Callanan76264762010-04-02 22:27:05 +0000298 /// getStartLoc - Get the location of the first token of this operand.
299 SMLoc getStartLoc() const { return StartLoc; }
300 /// getEndLoc - Get the location of the last token of this operand.
301 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000302
Daniel Dunbar8462b302010-08-11 06:36:53 +0000303 ARMCC::CondCodes getCondCode() const {
304 assert(Kind == CondCode && "Invalid access!");
305 return CC.Val;
306 }
307
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000308 unsigned getCoproc() const {
309 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
310 return Cop.Val;
311 }
312
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000313 StringRef getToken() const {
314 assert(Kind == Token && "Invalid access!");
315 return StringRef(Tok.Data, Tok.Length);
316 }
317
318 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000319 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000320 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000321 }
322
Bill Wendling5fa22a12010-11-09 23:28:44 +0000323 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000324 assert((Kind == RegisterList || Kind == DPRRegisterList ||
325 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000326 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000327 }
328
Kevin Enderbycfe07242009-10-13 22:19:02 +0000329 const MCExpr *getImm() const {
330 assert(Kind == Immediate && "Invalid access!");
331 return Imm.Val;
332 }
333
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000334 ARM_MB::MemBOpt getMemBarrierOpt() const {
335 assert(Kind == MemBarrierOpt && "Invalid access!");
336 return MBOpt.Val;
337 }
338
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000339 ARM_PROC::IFlags getProcIFlags() const {
340 assert(Kind == ProcIFlags && "Invalid access!");
341 return IFlags.Val;
342 }
343
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000344 unsigned getMSRMask() const {
345 assert(Kind == MSRMask && "Invalid access!");
346 return MMask.Val;
347 }
348
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000349 /// @name Memory Operand Accessors
350 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000351 ARMII::AddrMode getMemAddrMode() const {
352 return Mem.AddrMode;
353 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000354 unsigned getMemBaseRegNum() const {
355 return Mem.BaseRegNum;
356 }
357 unsigned getMemOffsetRegNum() const {
358 assert(Mem.OffsetIsReg && "Invalid access!");
359 return Mem.Offset.RegNum;
360 }
361 const MCExpr *getMemOffset() const {
362 assert(!Mem.OffsetIsReg && "Invalid access!");
363 return Mem.Offset.Value;
364 }
365 unsigned getMemOffsetRegShifted() const {
366 assert(Mem.OffsetIsReg && "Invalid access!");
367 return Mem.OffsetRegShifted;
368 }
369 const MCExpr *getMemShiftAmount() const {
370 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
371 return Mem.ShiftAmount;
372 }
Owen Anderson00828302011-03-18 22:50:18 +0000373 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000374 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
375 return Mem.ShiftType;
376 }
377 bool getMemPreindexed() const { return Mem.Preindexed; }
378 bool getMemPostindexed() const { return Mem.Postindexed; }
379 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
380 bool getMemNegative() const { return Mem.Negative; }
381 bool getMemWriteback() const { return Mem.Writeback; }
382
383 /// @}
384
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000385 bool isCoprocNum() const { return Kind == CoprocNum; }
386 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000387 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000388 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000389 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000390 bool isImm0_255() const {
391 if (Kind != Immediate)
392 return false;
393 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
394 if (!CE) return false;
395 int64_t Value = CE->getValue();
396 return Value >= 0 && Value < 256;
397 }
398 bool isT2SOImm() const {
399 if (Kind != Immediate)
400 return false;
401 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
402 if (!CE) return false;
403 int64_t Value = CE->getValue();
404 return ARM_AM::getT2SOImmVal(Value) != -1;
405 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000406 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000407 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000408 bool isDPRRegList() const { return Kind == DPRRegisterList; }
409 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000410 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000411 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000412 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000413 bool isShifter() const { return Kind == Shifter; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000414 bool isMemMode2() const {
415 if (getMemAddrMode() != ARMII::AddrMode2)
416 return false;
417
418 if (getMemOffsetIsReg())
419 return true;
420
421 if (getMemNegative() &&
422 !(getMemPostindexed() || getMemPreindexed()))
423 return false;
424
425 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
426 if (!CE) return false;
427 int64_t Value = CE->getValue();
428
429 // The offset must be in the range 0-4095 (imm12).
430 if (Value > 4095 || Value < -4095)
431 return false;
432
433 return true;
434 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000435 bool isMemMode3() const {
436 if (getMemAddrMode() != ARMII::AddrMode3)
437 return false;
438
439 if (getMemOffsetIsReg()) {
440 if (getMemOffsetRegShifted())
441 return false; // No shift with offset reg allowed
442 return true;
443 }
444
445 if (getMemNegative() &&
446 !(getMemPostindexed() || getMemPreindexed()))
447 return false;
448
449 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
450 if (!CE) return false;
451 int64_t Value = CE->getValue();
452
453 // The offset must be in the range 0-255 (imm8).
454 if (Value > 255 || Value < -255)
455 return false;
456
457 return true;
458 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000459 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000460 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
461 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000462 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000463
Daniel Dunbar4b462672011-01-18 05:55:27 +0000464 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000465 if (!CE) return false;
466
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000467 // The offset must be a multiple of 4 in the range 0-1020.
468 int64_t Value = CE->getValue();
469 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
470 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000471 bool isMemMode7() const {
472 if (!isMemory() ||
473 getMemPreindexed() ||
474 getMemPostindexed() ||
475 getMemOffsetIsReg() ||
476 getMemNegative() ||
477 getMemWriteback())
478 return false;
479
480 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
481 if (!CE) return false;
482
483 if (CE->getValue())
484 return false;
485
486 return true;
487 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000488 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000489 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000490 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000491 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000492 }
493 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000494 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000495 return false;
496
Daniel Dunbar4b462672011-01-18 05:55:27 +0000497 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000498 if (!CE) return false;
499
500 // The offset must be a multiple of 4 in the range 0-124.
501 uint64_t Value = CE->getValue();
502 return ((Value & 0x3) == 0 && Value <= 124);
503 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000504 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000505 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000506
507 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000508 // Add as immediates when possible. Null MCExpr = 0.
509 if (Expr == 0)
510 Inst.addOperand(MCOperand::CreateImm(0));
511 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000512 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
513 else
514 Inst.addOperand(MCOperand::CreateExpr(Expr));
515 }
516
Daniel Dunbar8462b302010-08-11 06:36:53 +0000517 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000518 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000519 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000520 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
521 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000522 }
523
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000524 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
525 assert(N == 1 && "Invalid number of operands!");
526 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
527 }
528
529 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
530 assert(N == 1 && "Invalid number of operands!");
531 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
532 }
533
Jim Grosbachd67641b2010-12-06 18:21:12 +0000534 void addCCOutOperands(MCInst &Inst, unsigned N) const {
535 assert(N == 1 && "Invalid number of operands!");
536 Inst.addOperand(MCOperand::CreateReg(getReg()));
537 }
538
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000539 void addRegOperands(MCInst &Inst, unsigned N) const {
540 assert(N == 1 && "Invalid number of operands!");
541 Inst.addOperand(MCOperand::CreateReg(getReg()));
542 }
543
Owen Anderson00828302011-03-18 22:50:18 +0000544 void addShifterOperands(MCInst &Inst, unsigned N) const {
545 assert(N == 1 && "Invalid number of operands!");
546 Inst.addOperand(MCOperand::CreateImm(
547 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
548 }
549
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000550 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000551 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000552 const SmallVectorImpl<unsigned> &RegList = getRegList();
553 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000554 I = RegList.begin(), E = RegList.end(); I != E; ++I)
555 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000556 }
557
Bill Wendling0f630752010-11-17 04:32:08 +0000558 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
559 addRegListOperands(Inst, N);
560 }
561
562 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
563 addRegListOperands(Inst, N);
564 }
565
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000566 void addImmOperands(MCInst &Inst, unsigned N) const {
567 assert(N == 1 && "Invalid number of operands!");
568 addExpr(Inst, getImm());
569 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000570
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000571 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
572 assert(N == 1 && "Invalid number of operands!");
573 addExpr(Inst, getImm());
574 }
575
576 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
577 assert(N == 1 && "Invalid number of operands!");
578 addExpr(Inst, getImm());
579 }
580
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000581 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
582 assert(N == 1 && "Invalid number of operands!");
583 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
584 }
585
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000586 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
587 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
588 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
589
590 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000591 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000592 assert((CE || CE->getValue() == 0) &&
593 "No offset operand support in mode 7");
594 }
595
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000596 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
597 assert(isMemMode2() && "Invalid mode or number of operands!");
598 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
599 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
600
601 if (getMemOffsetIsReg()) {
602 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
603
604 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
605 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
606 int64_t ShiftAmount = 0;
607
608 if (getMemOffsetRegShifted()) {
609 ShOpc = getMemShiftType();
610 const MCConstantExpr *CE =
611 dyn_cast<MCConstantExpr>(getMemShiftAmount());
612 ShiftAmount = CE->getValue();
613 }
614
615 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
616 ShOpc, IdxMode)));
617 return;
618 }
619
620 // Create a operand placeholder to always yield the same number of operands.
621 Inst.addOperand(MCOperand::CreateReg(0));
622
623 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
624 // the difference?
625 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
626 assert(CE && "Non-constant mode 2 offset operand!");
627 int64_t Offset = CE->getValue();
628
629 if (Offset >= 0)
630 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
631 Offset, ARM_AM::no_shift, IdxMode)));
632 else
633 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
634 -Offset, ARM_AM::no_shift, IdxMode)));
635 }
636
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000637 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
638 assert(isMemMode3() && "Invalid mode or number of operands!");
639 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
640 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
641
642 if (getMemOffsetIsReg()) {
643 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
644
645 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
646 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
647 IdxMode)));
648 return;
649 }
650
651 // Create a operand placeholder to always yield the same number of operands.
652 Inst.addOperand(MCOperand::CreateReg(0));
653
654 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
655 // the difference?
656 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
657 assert(CE && "Non-constant mode 3 offset operand!");
658 int64_t Offset = CE->getValue();
659
660 if (Offset >= 0)
661 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
662 Offset, IdxMode)));
663 else
664 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
665 -Offset, IdxMode)));
666 }
667
Chris Lattner14b93852010-10-29 00:27:31 +0000668 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
669 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000670
Daniel Dunbar4b462672011-01-18 05:55:27 +0000671 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
672 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000673
Jim Grosbach80eb2332010-10-29 17:41:25 +0000674 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
675 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000676 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000677 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000678
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000679 // The MCInst offset operand doesn't include the low two bits (like
680 // the instruction encoding).
681 int64_t Offset = CE->getValue() / 4;
682 if (Offset >= 0)
683 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
684 Offset)));
685 else
686 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
687 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000688 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000689
Bill Wendlingf4caf692010-12-14 03:36:38 +0000690 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
691 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000692 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
693 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000694 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000695
Bill Wendlingf4caf692010-12-14 03:36:38 +0000696 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
697 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000698 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
699 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000700 assert(CE && "Non-constant mode offset operand!");
701 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000702 }
703
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000704 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
705 assert(N == 1 && "Invalid number of operands!");
706 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
707 }
708
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000709 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
710 assert(N == 1 && "Invalid number of operands!");
711 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
712 }
713
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000714 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000715
Chris Lattner3a697562010-10-28 17:20:03 +0000716 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
717 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000718 Op->CC.Val = CC;
719 Op->StartLoc = S;
720 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000721 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000722 }
723
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000724 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
725 ARMOperand *Op = new ARMOperand(CoprocNum);
726 Op->Cop.Val = CopVal;
727 Op->StartLoc = S;
728 Op->EndLoc = S;
729 return Op;
730 }
731
732 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
733 ARMOperand *Op = new ARMOperand(CoprocReg);
734 Op->Cop.Val = CopVal;
735 Op->StartLoc = S;
736 Op->EndLoc = S;
737 return Op;
738 }
739
Jim Grosbachd67641b2010-12-06 18:21:12 +0000740 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
741 ARMOperand *Op = new ARMOperand(CCOut);
742 Op->Reg.RegNum = RegNum;
743 Op->StartLoc = S;
744 Op->EndLoc = S;
745 return Op;
746 }
747
Chris Lattner3a697562010-10-28 17:20:03 +0000748 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
749 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000750 Op->Tok.Data = Str.data();
751 Op->Tok.Length = Str.size();
752 Op->StartLoc = S;
753 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000754 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000755 }
756
Bill Wendling50d0f582010-11-18 23:43:05 +0000757 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000758 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000759 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000760 Op->StartLoc = S;
761 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000762 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000763 }
764
Owen Anderson00828302011-03-18 22:50:18 +0000765 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
766 SMLoc S, SMLoc E) {
767 ARMOperand *Op = new ARMOperand(Shifter);
768 Op->Shift.ShiftTy = ShTy;
769 Op->StartLoc = S;
770 Op->EndLoc = E;
771 return Op;
772 }
773
Bill Wendling7729e062010-11-09 22:44:22 +0000774 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000775 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000776 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000777 KindTy Kind = RegisterList;
778
779 if (ARM::DPRRegClass.contains(Regs.front().first))
780 Kind = DPRRegisterList;
781 else if (ARM::SPRRegClass.contains(Regs.front().first))
782 Kind = SPRRegisterList;
783
784 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000785 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000786 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000787 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000788 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000789 Op->StartLoc = StartLoc;
790 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000791 return Op;
792 }
793
Chris Lattner3a697562010-10-28 17:20:03 +0000794 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
795 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000796 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000797 Op->StartLoc = S;
798 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000799 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000800 }
801
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000802 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
803 bool OffsetIsReg, const MCExpr *Offset,
804 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000805 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000806 const MCExpr *ShiftAmount, bool Preindexed,
807 bool Postindexed, bool Negative, bool Writeback,
808 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000809 assert((OffsetRegNum == -1 || OffsetIsReg) &&
810 "OffsetRegNum must imply OffsetIsReg!");
811 assert((!OffsetRegShifted || OffsetIsReg) &&
812 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000813 assert((Offset || OffsetIsReg) &&
814 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000815 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
816 "Cannot have shift amount without shifted register offset!");
817 assert((!Offset || !OffsetIsReg) &&
818 "Cannot have expression offset and register offset!");
819
Chris Lattner3a697562010-10-28 17:20:03 +0000820 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000821 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000822 Op->Mem.BaseRegNum = BaseRegNum;
823 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000824 if (OffsetIsReg)
825 Op->Mem.Offset.RegNum = OffsetRegNum;
826 else
827 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000828 Op->Mem.OffsetRegShifted = OffsetRegShifted;
829 Op->Mem.ShiftType = ShiftType;
830 Op->Mem.ShiftAmount = ShiftAmount;
831 Op->Mem.Preindexed = Preindexed;
832 Op->Mem.Postindexed = Postindexed;
833 Op->Mem.Negative = Negative;
834 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000835
Sean Callanan76264762010-04-02 22:27:05 +0000836 Op->StartLoc = S;
837 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000838 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000839 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000840
841 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
842 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
843 Op->MBOpt.Val = Opt;
844 Op->StartLoc = S;
845 Op->EndLoc = S;
846 return Op;
847 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000848
849 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
850 ARMOperand *Op = new ARMOperand(ProcIFlags);
851 Op->IFlags.Val = IFlags;
852 Op->StartLoc = S;
853 Op->EndLoc = S;
854 return Op;
855 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000856
857 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
858 ARMOperand *Op = new ARMOperand(MSRMask);
859 Op->MMask.Val = MMask;
860 Op->StartLoc = S;
861 Op->EndLoc = S;
862 return Op;
863 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000864};
865
866} // end anonymous namespace.
867
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000868void ARMOperand::dump(raw_ostream &OS) const {
869 switch (Kind) {
870 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000871 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000872 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000873 case CCOut:
874 OS << "<ccout " << getReg() << ">";
875 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000876 case CoprocNum:
877 OS << "<coprocessor number: " << getCoproc() << ">";
878 break;
879 case CoprocReg:
880 OS << "<coprocessor register: " << getCoproc() << ">";
881 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000882 case MSRMask:
883 OS << "<mask: " << getMSRMask() << ">";
884 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000885 case Immediate:
886 getImm()->print(OS);
887 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000888 case MemBarrierOpt:
889 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
890 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000891 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000892 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000893 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
894 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000895 if (getMemOffsetIsReg()) {
896 OS << " offset:<register " << getMemOffsetRegNum();
897 if (getMemOffsetRegShifted()) {
898 OS << " offset-shift-type:" << getMemShiftType();
899 OS << " offset-shift-amount:" << *getMemShiftAmount();
900 }
901 } else {
902 OS << " offset:" << *getMemOffset();
903 }
904 if (getMemOffsetIsReg())
905 OS << " (offset-is-reg)";
906 if (getMemPreindexed())
907 OS << " (pre-indexed)";
908 if (getMemPostindexed())
909 OS << " (post-indexed)";
910 if (getMemNegative())
911 OS << " (negative)";
912 if (getMemWriteback())
913 OS << " (writeback)";
914 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000915 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000916 case ProcIFlags: {
917 OS << "<ARM_PROC::";
918 unsigned IFlags = getProcIFlags();
919 for (int i=2; i >= 0; --i)
920 if (IFlags & (1 << i))
921 OS << ARM_PROC::IFlagsToString(1 << i);
922 OS << ">";
923 break;
924 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000925 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000926 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000927 break;
Owen Anderson00828302011-03-18 22:50:18 +0000928 case Shifter:
929 OS << "<shifter " << getShiftOpcStr(Shift.ShiftTy) << ">";
930 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000931 case RegisterList:
932 case DPRRegisterList:
933 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000934 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000935
Bill Wendling5fa22a12010-11-09 23:28:44 +0000936 const SmallVectorImpl<unsigned> &RegList = getRegList();
937 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000938 I = RegList.begin(), E = RegList.end(); I != E; ) {
939 OS << *I;
940 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000941 }
942
943 OS << ">";
944 break;
945 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000946 case Token:
947 OS << "'" << getToken() << "'";
948 break;
949 }
950}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000951
952/// @name Auto-generated Match Functions
953/// {
954
955static unsigned MatchRegisterName(StringRef Name);
956
957/// }
958
Bob Wilson69df7232011-02-03 21:46:10 +0000959bool ARMAsmParser::ParseRegister(unsigned &RegNo,
960 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000961 RegNo = TryParseRegister();
962
963 return (RegNo == (unsigned)-1);
964}
965
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000966/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000967/// and if it is a register name the token is eaten and the register number is
968/// returned. Otherwise return -1.
969///
970int ARMAsmParser::TryParseRegister() {
971 const AsmToken &Tok = Parser.getTok();
972 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000973
Chris Lattnere5658fa2010-10-30 04:09:10 +0000974 // FIXME: Validate register for the current architecture; we have to do
975 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000976 std::string upperCase = Tok.getString().str();
977 std::string lowerCase = LowercaseString(upperCase);
978 unsigned RegNum = MatchRegisterName(lowerCase);
979 if (!RegNum) {
980 RegNum = StringSwitch<unsigned>(lowerCase)
981 .Case("r13", ARM::SP)
982 .Case("r14", ARM::LR)
983 .Case("r15", ARM::PC)
984 .Case("ip", ARM::R12)
985 .Default(0);
986 }
987 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000988
Chris Lattnere5658fa2010-10-30 04:09:10 +0000989 Parser.Lex(); // Eat identifier token.
990 return RegNum;
991}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000992
Owen Anderson00828302011-03-18 22:50:18 +0000993/// Try to parse a register name. The token must be an Identifier when called,
994/// and if it is a register name the token is eaten and the register number is
995/// returned. Otherwise return -1.
996///
997bool ARMAsmParser::TryParseShiftRegister(
998 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
999 SMLoc S = Parser.getTok().getLoc();
1000 const AsmToken &Tok = Parser.getTok();
1001 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1002
1003 std::string upperCase = Tok.getString().str();
1004 std::string lowerCase = LowercaseString(upperCase);
1005 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1006 .Case("lsl", ARM_AM::lsl)
1007 .Case("lsr", ARM_AM::lsr)
1008 .Case("asr", ARM_AM::asr)
1009 .Case("ror", ARM_AM::ror)
1010 .Case("rrx", ARM_AM::rrx)
1011 .Default(ARM_AM::no_shift);
1012
1013 if (ShiftTy == ARM_AM::no_shift)
1014 return true;
1015
1016 Parser.Lex(); // Eat shift-type operand;
1017 int RegNum = TryParseRegister();
1018 if (RegNum == -1)
1019 return Error(Parser.getTok().getLoc(), "register expected");
1020
1021 Operands.push_back(ARMOperand::CreateReg(RegNum,S, Parser.getTok().getLoc()));
1022 Operands.push_back(ARMOperand::CreateShifter(ShiftTy,
1023 S, Parser.getTok().getLoc()));
1024
1025 return false;
1026}
1027
1028
Bill Wendling50d0f582010-11-18 23:43:05 +00001029/// Try to parse a register name. The token must be an Identifier when called.
1030/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1031/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001032///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001033/// TODO this is likely to change to allow different register types and or to
1034/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001035bool ARMAsmParser::
1036TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001037 SMLoc S = Parser.getTok().getLoc();
1038 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001039 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001040 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001041
Bill Wendling50d0f582010-11-18 23:43:05 +00001042 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001043
Chris Lattnere5658fa2010-10-30 04:09:10 +00001044 const AsmToken &ExclaimTok = Parser.getTok();
1045 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001046 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1047 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001048 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001049 }
1050
Bill Wendling50d0f582010-11-18 23:43:05 +00001051 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001052}
1053
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001054/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1055/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1056/// "c5", ...
1057static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001058 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1059 // but efficient.
1060 switch (Name.size()) {
1061 default: break;
1062 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001063 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001064 return -1;
1065 switch (Name[1]) {
1066 default: return -1;
1067 case '0': return 0;
1068 case '1': return 1;
1069 case '2': return 2;
1070 case '3': return 3;
1071 case '4': return 4;
1072 case '5': return 5;
1073 case '6': return 6;
1074 case '7': return 7;
1075 case '8': return 8;
1076 case '9': return 9;
1077 }
1078 break;
1079 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001080 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001081 return -1;
1082 switch (Name[2]) {
1083 default: return -1;
1084 case '0': return 10;
1085 case '1': return 11;
1086 case '2': return 12;
1087 case '3': return 13;
1088 case '4': return 14;
1089 case '5': return 15;
1090 }
1091 break;
1092 }
1093
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001094 return -1;
1095}
1096
Jim Grosbachf922c472011-02-12 01:34:40 +00001097/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001098/// token must be an Identifier when called, and if it is a coprocessor
1099/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001100ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1101tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001102 SMLoc S = Parser.getTok().getLoc();
1103 const AsmToken &Tok = Parser.getTok();
1104 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1105
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001106 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001107 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001108 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001109
1110 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001111 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001112 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001113}
1114
Jim Grosbachf922c472011-02-12 01:34:40 +00001115/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001116/// token must be an Identifier when called, and if it is a coprocessor
1117/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001118ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1119tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001120 SMLoc S = Parser.getTok().getLoc();
1121 const AsmToken &Tok = Parser.getTok();
1122 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1123
1124 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1125 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001126 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001127
1128 Parser.Lex(); // Eat identifier token.
1129 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001130 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001131}
1132
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001133/// Parse a register list, return it if successful else return null. The first
1134/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001135bool ARMAsmParser::
1136ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001137 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001138 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001139 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001140
Bill Wendling7729e062010-11-09 22:44:22 +00001141 // Read the rest of the registers in the list.
1142 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001143 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001144
Bill Wendling7729e062010-11-09 22:44:22 +00001145 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001146 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001147 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001148
Sean Callanan18b83232010-01-19 21:44:56 +00001149 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001150 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001151 if (RegTok.isNot(AsmToken::Identifier)) {
1152 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001153 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001154 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001155
Bill Wendling1d6a2652010-11-06 10:40:24 +00001156 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001157 if (RegNum == -1) {
1158 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001159 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001160 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001161
Bill Wendlinge7176102010-11-06 22:36:58 +00001162 if (IsRange) {
1163 int Reg = PrevRegNum;
1164 do {
1165 ++Reg;
1166 Registers.push_back(std::make_pair(Reg, RegLoc));
1167 } while (Reg != RegNum);
1168 } else {
1169 Registers.push_back(std::make_pair(RegNum, RegLoc));
1170 }
1171
1172 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001173 } while (Parser.getTok().is(AsmToken::Comma) ||
1174 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001175
1176 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001177 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001178 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1179 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001180 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001181 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001182
Bill Wendlinge7176102010-11-06 22:36:58 +00001183 SMLoc E = RCurlyTok.getLoc();
1184 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001185
Bill Wendlinge7176102010-11-06 22:36:58 +00001186 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001187 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001188 RI = Registers.begin(), RE = Registers.end();
1189
Bill Wendling7caebff2011-01-12 21:20:59 +00001190 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001191 bool EmittedWarning = false;
1192
Bill Wendling7caebff2011-01-12 21:20:59 +00001193 DenseMap<unsigned, bool> RegMap;
1194 RegMap[HighRegNum] = true;
1195
Bill Wendlinge7176102010-11-06 22:36:58 +00001196 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001197 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001198 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001199
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001200 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001201 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001202 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001203 }
1204
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001205 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001206 Warning(RegInfo.second,
1207 "register not in ascending order in register list");
1208
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001209 RegMap[Reg] = true;
1210 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001211 }
1212
Bill Wendling50d0f582010-11-18 23:43:05 +00001213 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1214 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001215}
1216
Jim Grosbachf922c472011-02-12 01:34:40 +00001217/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1218ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1219tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001220 SMLoc S = Parser.getTok().getLoc();
1221 const AsmToken &Tok = Parser.getTok();
1222 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1223 StringRef OptStr = Tok.getString();
1224
1225 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1226 .Case("sy", ARM_MB::SY)
1227 .Case("st", ARM_MB::ST)
1228 .Case("ish", ARM_MB::ISH)
1229 .Case("ishst", ARM_MB::ISHST)
1230 .Case("nsh", ARM_MB::NSH)
1231 .Case("nshst", ARM_MB::NSHST)
1232 .Case("osh", ARM_MB::OSH)
1233 .Case("oshst", ARM_MB::OSHST)
1234 .Default(~0U);
1235
1236 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001237 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001238
1239 Parser.Lex(); // Eat identifier token.
1240 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001241 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001242}
1243
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001244/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001245ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1246tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1247 SMLoc S = Parser.getTok().getLoc();
1248 const AsmToken &Tok = Parser.getTok();
1249 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1250 StringRef IFlagsStr = Tok.getString();
1251
1252 unsigned IFlags = 0;
1253 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1254 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1255 .Case("a", ARM_PROC::A)
1256 .Case("i", ARM_PROC::I)
1257 .Case("f", ARM_PROC::F)
1258 .Default(~0U);
1259
1260 // If some specific iflag is already set, it means that some letter is
1261 // present more than once, this is not acceptable.
1262 if (Flag == ~0U || (IFlags & Flag))
1263 return MatchOperand_NoMatch;
1264
1265 IFlags |= Flag;
1266 }
1267
1268 Parser.Lex(); // Eat identifier token.
1269 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1270 return MatchOperand_Success;
1271}
1272
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001273/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1274ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1275tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1276 SMLoc S = Parser.getTok().getLoc();
1277 const AsmToken &Tok = Parser.getTok();
1278 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1279 StringRef Mask = Tok.getString();
1280
1281 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1282 size_t Start = 0, Next = Mask.find('_');
1283 StringRef Flags = "";
1284 StringRef SpecReg = Mask.slice(Start, Next);
1285 if (Next != StringRef::npos)
1286 Flags = Mask.slice(Next+1, Mask.size());
1287
1288 // FlagsVal contains the complete mask:
1289 // 3-0: Mask
1290 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1291 unsigned FlagsVal = 0;
1292
1293 if (SpecReg == "apsr") {
1294 FlagsVal = StringSwitch<unsigned>(Flags)
1295 .Case("nzcvq", 0x8) // same as CPSR_c
1296 .Case("g", 0x4) // same as CPSR_s
1297 .Case("nzcvqg", 0xc) // same as CPSR_fs
1298 .Default(~0U);
1299
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001300 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001301 if (!Flags.empty())
1302 return MatchOperand_NoMatch;
1303 else
1304 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001305 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001306 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001307 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1308 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001309 for (int i = 0, e = Flags.size(); i != e; ++i) {
1310 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1311 .Case("c", 1)
1312 .Case("x", 2)
1313 .Case("s", 4)
1314 .Case("f", 8)
1315 .Default(~0U);
1316
1317 // If some specific flag is already set, it means that some letter is
1318 // present more than once, this is not acceptable.
1319 if (FlagsVal == ~0U || (FlagsVal & Flag))
1320 return MatchOperand_NoMatch;
1321 FlagsVal |= Flag;
1322 }
1323 } else // No match for special register.
1324 return MatchOperand_NoMatch;
1325
1326 // Special register without flags are equivalent to "fc" flags.
1327 if (!FlagsVal)
1328 FlagsVal = 0x9;
1329
1330 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1331 if (SpecReg == "spsr")
1332 FlagsVal |= 16;
1333
1334 Parser.Lex(); // Eat identifier token.
1335 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1336 return MatchOperand_Success;
1337}
1338
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001339/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1340ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1341tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001342 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001343
1344 if (ParseMemory(Operands, ARMII::AddrMode2))
1345 return MatchOperand_NoMatch;
1346
1347 return MatchOperand_Success;
1348}
1349
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001350/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1351ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1352tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1353 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1354
1355 if (ParseMemory(Operands, ARMII::AddrMode3))
1356 return MatchOperand_NoMatch;
1357
1358 return MatchOperand_Success;
1359}
1360
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001361/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1362/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1363/// when they refer multiple MIOperands inside a single one.
1364bool ARMAsmParser::
1365CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1366 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1367 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1368
1369 // Create a writeback register dummy placeholder.
1370 Inst.addOperand(MCOperand::CreateImm(0));
1371
1372 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1373 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1374 return true;
1375}
1376
1377/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1378/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1379/// when they refer multiple MIOperands inside a single one.
1380bool ARMAsmParser::
1381CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1382 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1383 // Create a writeback register dummy placeholder.
1384 Inst.addOperand(MCOperand::CreateImm(0));
1385 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1386 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1387 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1388 return true;
1389}
1390
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001391/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1392/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1393/// when they refer multiple MIOperands inside a single one.
1394bool ARMAsmParser::
1395CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1396 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1397 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1398
1399 // Create a writeback register dummy placeholder.
1400 Inst.addOperand(MCOperand::CreateImm(0));
1401
1402 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1403 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1404 return true;
1405}
1406
1407/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1408/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1409/// when they refer multiple MIOperands inside a single one.
1410bool ARMAsmParser::
1411CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1412 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1413 // Create a writeback register dummy placeholder.
1414 Inst.addOperand(MCOperand::CreateImm(0));
1415 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1416 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1417 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1418 return true;
1419}
1420
Bill Wendlinge7176102010-11-06 22:36:58 +00001421/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001422/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001423///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001424/// TODO Only preindexing and postindexing addressing are started, unindexed
1425/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001426bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001427ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1428 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001429 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001430 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001431 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001432 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001433 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001434
Sean Callanan18b83232010-01-19 21:44:56 +00001435 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001436 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1437 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001438 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001439 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001440 int BaseRegNum = TryParseRegister();
1441 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001442 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001443 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001444 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001445
Daniel Dunbar05710932011-01-18 05:34:17 +00001446 // The next token must either be a comma or a closing bracket.
1447 const AsmToken &Tok = Parser.getTok();
1448 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1449 return true;
1450
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001451 bool Preindexed = false;
1452 bool Postindexed = false;
1453 bool OffsetIsReg = false;
1454 bool Negative = false;
1455 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001456 ARMOperand *WBOp = 0;
1457 int OffsetRegNum = -1;
1458 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001459 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001460 const MCExpr *ShiftAmount = 0;
1461 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001462
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001463 // First look for preindexed address forms, that is after the "[Rn" we now
1464 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001465 if (Tok.is(AsmToken::Comma)) {
1466 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001467 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001468
Chris Lattner550276e2010-10-28 20:52:15 +00001469 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1470 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001471 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001472 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001473 if (RBracTok.isNot(AsmToken::RBrac)) {
1474 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001475 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001476 }
Sean Callanan76264762010-04-02 22:27:05 +00001477 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001478 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001479
Sean Callanan18b83232010-01-19 21:44:56 +00001480 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001481 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001482 // None of addrmode3 instruction uses "!"
1483 if (AddrMode == ARMII::AddrMode3)
1484 return true;
1485
Bill Wendling50d0f582010-11-18 23:43:05 +00001486 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1487 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001488 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001489 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001490 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1491 if (AddrMode == ARMII::AddrMode2)
1492 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001493 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001494 } else {
1495 // The "[Rn" we have so far was not followed by a comma.
1496
Jim Grosbach80eb2332010-10-29 17:41:25 +00001497 // If there's anything other than the right brace, this is a post indexing
1498 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001499 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001500 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001501
Sean Callanan18b83232010-01-19 21:44:56 +00001502 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001503
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001504 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001505 Postindexed = true;
1506 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001507
Chris Lattner550276e2010-10-28 20:52:15 +00001508 if (NextTok.isNot(AsmToken::Comma)) {
1509 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001510 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001511 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001512
Sean Callananb9a25b72010-01-19 20:27:46 +00001513 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001514
Chris Lattner550276e2010-10-28 20:52:15 +00001515 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001516 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001517 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001518 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001519 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001520 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001521
1522 // Force Offset to exist if used.
1523 if (!OffsetIsReg) {
1524 if (!Offset)
1525 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001526 } else {
1527 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1528 Error(E, "shift amount not supported");
1529 return true;
1530 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001531 }
1532
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001533 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1534 Offset, OffsetRegNum, OffsetRegShifted,
1535 ShiftType, ShiftAmount, Preindexed,
1536 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001537 if (WBOp)
1538 Operands.push_back(WBOp);
1539
1540 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001541}
1542
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001543/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1544/// we will parse the following (were +/- means that a plus or minus is
1545/// optional):
1546/// +/-Rm
1547/// +/-Rm, shift
1548/// #offset
1549/// we return false on success or an error otherwise.
1550bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001551 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001552 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001553 const MCExpr *&ShiftAmount,
1554 const MCExpr *&Offset,
1555 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001556 int &OffsetRegNum,
1557 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001558 Negative = false;
1559 OffsetRegShifted = false;
1560 OffsetIsReg = false;
1561 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001562 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001563 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001564 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001565 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001566 else if (NextTok.is(AsmToken::Minus)) {
1567 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001568 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001569 }
1570 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001571 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001572 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001573 SMLoc CurLoc = OffsetRegTok.getLoc();
1574 OffsetRegNum = TryParseRegister();
1575 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001576 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001577 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001578 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001579 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001580
Bill Wendling12f40e92010-11-06 10:51:53 +00001581 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001582 if (OffsetRegNum != -1) {
1583 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001584 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001585 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001586 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001587
Sean Callanan18b83232010-01-19 21:44:56 +00001588 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001589 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001590 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001591 OffsetRegShifted = true;
1592 }
1593 }
1594 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1595 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001596 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001597 if (HashTok.isNot(AsmToken::Hash))
1598 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001599
Sean Callananb9a25b72010-01-19 20:27:46 +00001600 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001601
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001602 if (getParser().ParseExpression(Offset))
1603 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001604 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001605 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001606 return false;
1607}
1608
1609/// ParseShift as one of these two:
1610/// ( lsl | lsr | asr | ror ) , # shift_amount
1611/// rrx
1612/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001613bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1614 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001615 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001616 if (Tok.isNot(AsmToken::Identifier))
1617 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001618 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001619 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001620 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001621 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001622 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001623 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001624 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001625 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001626 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001627 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001628 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001629 else
1630 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001631 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001632
1633 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001634 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001635 return false;
1636
1637 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001638 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001639 if (HashTok.isNot(AsmToken::Hash))
1640 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001641 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001642
1643 if (getParser().ParseExpression(ShiftAmount))
1644 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001645
1646 return false;
1647}
1648
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001649/// Parse a arm instruction operand. For now this parses the operand regardless
1650/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001651bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001652 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001653 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001654
1655 // Check if the current operand has a custom associated parser, if so, try to
1656 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001657 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1658 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001659 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001660 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1661 // there was a match, but an error occurred, in which case, just return that
1662 // the operand parsing failed.
1663 if (ResTy == MatchOperand_ParseFail)
1664 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001665
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001666 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001667 default:
1668 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001669 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001670 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001671 if (!TryParseRegisterWithWriteBack(Operands))
1672 return false;
Owen Anderson00828302011-03-18 22:50:18 +00001673 if (!TryParseShiftRegister(Operands))
1674 return false;
1675
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001676
1677 // Fall though for the Identifier case that is not a register or a
1678 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001679 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1680 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001681 // This was not a register so parse other operands that start with an
1682 // identifier (like labels) as expressions and create them as immediates.
1683 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001684 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001685 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001686 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001687 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001688 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1689 return false;
1690 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001691 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001692 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001693 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001694 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001695 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001696 // #42 -> immediate.
1697 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001698 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001699 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001700 const MCExpr *ImmVal;
1701 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001702 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001703 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001704 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1705 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001706 case AsmToken::Colon: {
1707 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001708 // FIXME: Check it's an expression prefix,
1709 // e.g. (FOO - :lower16:BAR) isn't legal.
1710 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001711 if (ParsePrefix(RefKind))
1712 return true;
1713
Evan Cheng75972122011-01-13 07:58:56 +00001714 const MCExpr *SubExprVal;
1715 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001716 return true;
1717
Evan Cheng75972122011-01-13 07:58:56 +00001718 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1719 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001720 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001721 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001722 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001723 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001724 }
1725}
1726
Evan Cheng75972122011-01-13 07:58:56 +00001727// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1728// :lower16: and :upper16:.
1729bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1730 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001731
1732 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001733 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001734 Parser.Lex(); // Eat ':'
1735
1736 if (getLexer().isNot(AsmToken::Identifier)) {
1737 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1738 return true;
1739 }
1740
1741 StringRef IDVal = Parser.getTok().getIdentifier();
1742 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001743 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001744 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001745 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001746 } else {
1747 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1748 return true;
1749 }
1750 Parser.Lex();
1751
1752 if (getLexer().isNot(AsmToken::Colon)) {
1753 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1754 return true;
1755 }
1756 Parser.Lex(); // Eat the last ':'
1757 return false;
1758}
1759
1760const MCExpr *
1761ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1762 MCSymbolRefExpr::VariantKind Variant) {
1763 // Recurse over the given expression, rebuilding it to apply the given variant
1764 // to the leftmost symbol.
1765 if (Variant == MCSymbolRefExpr::VK_None)
1766 return E;
1767
1768 switch (E->getKind()) {
1769 case MCExpr::Target:
1770 llvm_unreachable("Can't handle target expr yet");
1771 case MCExpr::Constant:
1772 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1773
1774 case MCExpr::SymbolRef: {
1775 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1776
1777 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1778 return 0;
1779
1780 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1781 }
1782
1783 case MCExpr::Unary:
1784 llvm_unreachable("Can't handle unary expressions yet");
1785
1786 case MCExpr::Binary: {
1787 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1788 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1789 const MCExpr *RHS = BE->getRHS();
1790 if (!LHS)
1791 return 0;
1792
1793 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1794 }
1795 }
1796
1797 assert(0 && "Invalid expression kind!");
1798 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001799}
1800
Daniel Dunbar352e1482011-01-11 15:59:50 +00001801/// \brief Given a mnemonic, split out possible predication code and carry
1802/// setting letters to form a canonical mnemonic and flags.
1803//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001804// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001805static StringRef SplitMnemonic(StringRef Mnemonic,
1806 unsigned &PredicationCode,
1807 bool &CarrySetting,
1808 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001809 PredicationCode = ARMCC::AL;
1810 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001811 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001812
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001813 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001814 //
1815 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001816 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1817 Mnemonic == "movs" ||
1818 Mnemonic == "svc" ||
1819 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1820 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1821 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1822 Mnemonic == "vclt" ||
1823 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1824 Mnemonic == "vcle" ||
1825 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1826 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbachd1f0bbe2011-06-27 20:59:10 +00001827 Mnemonic == "vqdmlal" || Mnemonic == "bics"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001828 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001829
Daniel Dunbar352e1482011-01-11 15:59:50 +00001830 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001831 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001832 .Case("eq", ARMCC::EQ)
1833 .Case("ne", ARMCC::NE)
1834 .Case("hs", ARMCC::HS)
Jim Grosbach660a9ec2011-06-27 20:40:29 +00001835 .Case("cs", ARMCC::HS)
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001836 .Case("lo", ARMCC::LO)
Jim Grosbach660a9ec2011-06-27 20:40:29 +00001837 .Case("cc", ARMCC::LO)
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001838 .Case("mi", ARMCC::MI)
1839 .Case("pl", ARMCC::PL)
1840 .Case("vs", ARMCC::VS)
1841 .Case("vc", ARMCC::VC)
1842 .Case("hi", ARMCC::HI)
1843 .Case("ls", ARMCC::LS)
1844 .Case("ge", ARMCC::GE)
1845 .Case("lt", ARMCC::LT)
1846 .Case("gt", ARMCC::GT)
1847 .Case("le", ARMCC::LE)
1848 .Case("al", ARMCC::AL)
1849 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001850 if (CC != ~0U) {
1851 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001852 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001853 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001854
Daniel Dunbar352e1482011-01-11 15:59:50 +00001855 // Next, determine if we have a carry setting bit. We explicitly ignore all
1856 // the instructions we know end in 's'.
1857 if (Mnemonic.endswith("s") &&
1858 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1859 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1860 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1861 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1862 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1863 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1864 CarrySetting = true;
1865 }
1866
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001867 // The "cps" instruction can have a interrupt mode operand which is glued into
1868 // the mnemonic. Check if this is the case, split it and parse the imod op
1869 if (Mnemonic.startswith("cps")) {
1870 // Split out any imod code.
1871 unsigned IMod =
1872 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1873 .Case("ie", ARM_PROC::IE)
1874 .Case("id", ARM_PROC::ID)
1875 .Default(~0U);
1876 if (IMod != ~0U) {
1877 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1878 ProcessorIMod = IMod;
1879 }
1880 }
1881
Daniel Dunbar352e1482011-01-11 15:59:50 +00001882 return Mnemonic;
1883}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001884
1885/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1886/// inclusion of carry set or predication code operands.
1887//
1888// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001889void ARMAsmParser::
1890GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1891 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001892 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1893 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1894 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1895 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001896 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001897 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1898 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001899 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00001900 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001901 CanAcceptCarrySet = true;
1902 } else {
1903 CanAcceptCarrySet = false;
1904 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001905
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001906 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1907 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1908 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1909 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001910 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001911 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001912 CanAcceptPredicationCode = false;
1913 } else {
1914 CanAcceptPredicationCode = true;
1915 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001916
Evan Chengebdeeab2011-07-08 01:53:10 +00001917 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001918 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001919 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001920 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001921}
1922
1923/// Parse an arm instruction mnemonic followed by its operands.
1924bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1925 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1926 // Create the leading tokens for the mnemonic, split by '.' characters.
1927 size_t Start = 0, Next = Name.find('.');
1928 StringRef Head = Name.slice(Start, Next);
1929
Daniel Dunbar352e1482011-01-11 15:59:50 +00001930 // Split out the predication code and carry setting flag from the mnemonic.
1931 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001932 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001933 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001934 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1935 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001936
Chris Lattner3a697562010-10-28 17:20:03 +00001937 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001938
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001939 // Next, add the CCOut and ConditionCode operands, if needed.
1940 //
1941 // For mnemonics which can ever incorporate a carry setting bit or predication
1942 // code, our matching model involves us always generating CCOut and
1943 // ConditionCode operands to match the mnemonic "as written" and then we let
1944 // the matcher deal with finding the right instruction or generating an
1945 // appropriate error.
1946 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1947 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1948
1949 // Add the carry setting operand, if necessary.
1950 //
1951 // FIXME: It would be awesome if we could somehow invent a location such that
1952 // match errors on this operand would print a nice diagnostic about how the
1953 // 's' character in the mnemonic resulted in a CCOut operand.
1954 if (CanAcceptCarrySet) {
1955 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1956 NameLoc));
1957 } else {
1958 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1959 // misspelled another mnemonic).
1960
1961 // FIXME: Issue a nice error.
1962 }
1963
1964 // Add the predication code operand, if necessary.
1965 if (CanAcceptPredicationCode) {
1966 Operands.push_back(ARMOperand::CreateCondCode(
1967 ARMCC::CondCodes(PredicationCode), NameLoc));
1968 } else {
1969 // This mnemonic can't ever accept a predication code, but the user wrote
1970 // one (or misspelled another mnemonic).
1971
1972 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001973 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001974
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001975 // Add the processor imod operand, if necessary.
1976 if (ProcessorIMod) {
1977 Operands.push_back(ARMOperand::CreateImm(
1978 MCConstantExpr::Create(ProcessorIMod, getContext()),
1979 NameLoc, NameLoc));
1980 } else {
1981 // This mnemonic can't ever accept a imod, but the user wrote
1982 // one (or misspelled another mnemonic).
1983
1984 // FIXME: Issue a nice error.
1985 }
1986
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001987 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001988 while (Next != StringRef::npos) {
1989 Start = Next;
1990 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001991 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001992
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001993 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001994 }
1995
1996 // Read the remaining operands.
1997 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001998 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001999 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002000 Parser.EatToEndOfStatement();
2001 return true;
2002 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002003
2004 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002005 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002006
2007 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002008 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002009 Parser.EatToEndOfStatement();
2010 return true;
2011 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002012 }
2013 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002014
Chris Lattnercbf8a982010-09-11 16:18:25 +00002015 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2016 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002017 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002018 }
Bill Wendling146018f2010-11-06 21:42:12 +00002019
Chris Lattner34e53142010-09-08 05:10:46 +00002020 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00002021 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002022}
2023
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002024bool ARMAsmParser::
2025MatchAndEmitInstruction(SMLoc IDLoc,
2026 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2027 MCStreamer &Out) {
2028 MCInst Inst;
2029 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002030 MatchResultTy MatchResult, MatchResult2;
2031 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2032 if (MatchResult != Match_Success) {
2033 // If we get a Match_InvalidOperand it might be some arithmetic instruction
2034 // that does not update the condition codes. So try adding a CCOut operand
2035 // with a value of reg0.
2036 if (MatchResult == Match_InvalidOperand) {
2037 Operands.insert(Operands.begin() + 1,
2038 ARMOperand::CreateCCOut(0,
2039 ((ARMOperand*)Operands[0])->getStartLoc()));
2040 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2041 if (MatchResult2 == Match_Success)
2042 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002043 else {
2044 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002045 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002046 delete CCOut;
2047 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002048 }
2049 // If we get a Match_MnemonicFail it might be some arithmetic instruction
2050 // that updates the condition codes if it ends in 's'. So see if the
2051 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
2052 // operand with a value of CPSR.
Evan Chengeb0caa12011-07-08 22:49:55 +00002053 else if (MatchResult == Match_MnemonicFail) {
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002054 // Get the instruction mnemonic, which is the first token.
2055 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
2056 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
2057 // removed the 's' from the mnemonic for matching.
2058 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
2059 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002060 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2061 Operands.erase(Operands.begin());
2062 delete OldMnemonic;
2063 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002064 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
2065 Operands.insert(Operands.begin() + 1,
2066 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2067 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2068 if (MatchResult2 == Match_Success)
2069 MatchResult = Match_Success;
2070 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002071 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2072 Operands.erase(Operands.begin());
2073 delete OldMnemonic;
2074 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002075 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002076 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2077 Operands.erase(Operands.begin() + 1);
2078 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002079 }
2080 }
2081 }
2082 }
2083 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002084 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002085 Out.EmitInstruction(Inst);
2086 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002087 case Match_MissingFeature:
2088 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2089 return true;
2090 case Match_InvalidOperand: {
2091 SMLoc ErrorLoc = IDLoc;
2092 if (ErrorInfo != ~0U) {
2093 if (ErrorInfo >= Operands.size())
2094 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002095
Chris Lattnere73d4f82010-10-28 21:41:58 +00002096 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2097 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2098 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002099
Chris Lattnere73d4f82010-10-28 21:41:58 +00002100 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002101 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002102 case Match_MnemonicFail:
2103 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002104 case Match_ConversionFail:
2105 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002106 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002107
Eric Christopherc223e2b2010-10-29 09:26:59 +00002108 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002109 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002110}
2111
Kevin Enderby515d5092009-10-15 20:48:48 +00002112/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002113bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2114 StringRef IDVal = DirectiveID.getIdentifier();
2115 if (IDVal == ".word")
2116 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002117 else if (IDVal == ".thumb")
2118 return ParseDirectiveThumb(DirectiveID.getLoc());
2119 else if (IDVal == ".thumb_func")
2120 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2121 else if (IDVal == ".code")
2122 return ParseDirectiveCode(DirectiveID.getLoc());
2123 else if (IDVal == ".syntax")
2124 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002125 return true;
2126}
2127
2128/// ParseDirectiveWord
2129/// ::= .word [ expression (, expression)* ]
2130bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2131 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2132 for (;;) {
2133 const MCExpr *Value;
2134 if (getParser().ParseExpression(Value))
2135 return true;
2136
Chris Lattneraaec2052010-01-19 19:46:13 +00002137 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002138
2139 if (getLexer().is(AsmToken::EndOfStatement))
2140 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002141
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002142 // FIXME: Improve diagnostic.
2143 if (getLexer().isNot(AsmToken::Comma))
2144 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002145 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002146 }
2147 }
2148
Sean Callananb9a25b72010-01-19 20:27:46 +00002149 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002150 return false;
2151}
2152
Kevin Enderby515d5092009-10-15 20:48:48 +00002153/// ParseDirectiveThumb
2154/// ::= .thumb
2155bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2156 if (getLexer().isNot(AsmToken::EndOfStatement))
2157 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002158 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002159
2160 // TODO: set thumb mode
2161 // TODO: tell the MC streamer the mode
2162 // getParser().getStreamer().Emit???();
2163 return false;
2164}
2165
2166/// ParseDirectiveThumbFunc
2167/// ::= .thumbfunc symbol_name
2168bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002169 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2170 bool isMachO = MAI.hasSubsectionsViaSymbols();
2171 StringRef Name;
2172
2173 // Darwin asm has function name after .thumb_func direction
2174 // ELF doesn't
2175 if (isMachO) {
2176 const AsmToken &Tok = Parser.getTok();
2177 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2178 return Error(L, "unexpected token in .thumb_func directive");
2179 Name = Tok.getString();
2180 Parser.Lex(); // Consume the identifier token.
2181 }
2182
Kevin Enderby515d5092009-10-15 20:48:48 +00002183 if (getLexer().isNot(AsmToken::EndOfStatement))
2184 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002185 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002186
Rafael Espindola64695402011-05-16 16:17:21 +00002187 // FIXME: assuming function name will be the line following .thumb_func
2188 if (!isMachO) {
2189 Name = Parser.getTok().getString();
2190 }
2191
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002192 // Mark symbol as a thumb symbol.
2193 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2194 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002195 return false;
2196}
2197
2198/// ParseDirectiveSyntax
2199/// ::= .syntax unified | divided
2200bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002201 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002202 if (Tok.isNot(AsmToken::Identifier))
2203 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002204 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002205 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002206 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002207 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002208 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002209 else
2210 return Error(L, "unrecognized syntax mode in .syntax directive");
2211
2212 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002213 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002214 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002215
2216 // TODO tell the MC streamer the mode
2217 // getParser().getStreamer().Emit???();
2218 return false;
2219}
2220
2221/// ParseDirectiveCode
2222/// ::= .code 16 | 32
2223bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002224 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002225 if (Tok.isNot(AsmToken::Integer))
2226 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002227 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002228 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002229 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002230 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002231 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002232 else
2233 return Error(L, "invalid operand to .code directive");
2234
2235 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002236 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002237 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002238
Evan Cheng32869202011-07-08 22:36:29 +00002239 if (Val == 16) {
2240 if (!isThumb()) SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002241 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002242 } else {
2243 if (isThumb()) SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002244 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002245 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002246
Kevin Enderby515d5092009-10-15 20:48:48 +00002247 return false;
2248}
2249
Sean Callanan90b70972010-04-07 20:29:34 +00002250extern "C" void LLVMInitializeARMAsmLexer();
2251
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002252/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002253extern "C" void LLVMInitializeARMAsmParser() {
2254 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2255 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002256 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002257}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002258
Chris Lattner0692ee62010-09-06 19:11:01 +00002259#define GET_REGISTER_MATCHER
2260#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002261#include "ARMGenAsmMatcher.inc"