blob: 6952c384f4441b49c88f2c2eb0bb3c20c74bb37e [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"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000023#include "llvm/Target/TargetRegistry.h"
24#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000025#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000027#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000028#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000029#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000030#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000031using namespace llvm;
32
Chris Lattner3a697562010-10-28 17:20:03 +000033namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000034
35class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000036
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000037class ARMAsmParser : public TargetAsmParser {
38 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000039 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000040
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000041 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
44 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
46
Chris Lattnere5658fa2010-10-30 04:09:10 +000047 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000048 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000049 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Anderson00828302011-03-18 22:50:18 +000050 bool TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000051 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000052 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
53 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000054 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000055 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000056 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
57 MCSymbolRefExpr::VariantKind Variant);
58
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000059
Kevin Enderby9c41fa82009-10-30 22:55:57 +000060 bool ParseMemoryOffsetReg(bool &Negative,
61 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000062 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000063 const MCExpr *&ShiftAmount,
64 const MCExpr *&Offset,
65 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000066 int &OffsetRegNum,
67 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000068 bool ParseShift(enum ARM_AM::ShiftOpc &St,
69 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000070 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000072 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000073 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000074 bool ParseDirectiveSyntax(SMLoc L);
75
Chris Lattner7036f8b2010-09-29 01:42:58 +000076 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000077 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000078 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000079 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
80 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000081
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000082 /// @name Auto-generated Match Functions
83 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000084
Chris Lattner0692ee62010-09-06 19:11:01 +000085#define GET_ASSEMBLER_HEADER
86#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000087
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000088 /// }
89
Jim Grosbachf922c472011-02-12 01:34:40 +000090 OperandMatchResultTy tryParseCoprocNumOperand(
91 SmallVectorImpl<MCParsedAsmOperand*>&);
92 OperandMatchResultTy tryParseCoprocRegOperand(
93 SmallVectorImpl<MCParsedAsmOperand*>&);
94 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000095 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +000096 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000097 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +000098 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000099 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000100 OperandMatchResultTy tryParseMemMode2Operand(
101 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000102 OperandMatchResultTy tryParseMemMode3Operand(
103 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000104
105 // Asm Match Converter Methods
106 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
107 const SmallVectorImpl<MCParsedAsmOperand*> &);
108 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
109 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000110 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
111 const SmallVectorImpl<MCParsedAsmOperand*> &);
112 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
113 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000114
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000115public:
Daniel Dunbard73ada72010-07-19 00:33:49 +0000116 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +0000117 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
Sean Callananf6d91092011-04-18 20:20:44 +0000118 MCAsmParserExtension::Initialize(_Parser);
Jim Grosbach833c93c2010-11-01 16:59:54 +0000119 // Initialize the set of available features.
120 setAvailableFeatures(ComputeAvailableFeatures(
121 &TM.getSubtarget<ARMSubtarget>()));
122 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000123
Benjamin Kramer38e59892010-07-14 22:38:02 +0000124 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000125 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000126 virtual bool ParseDirective(AsmToken DirectiveID);
127};
Jim Grosbach16c74252010-10-29 14:46:02 +0000128} // end anonymous namespace
129
Chris Lattner3a697562010-10-28 17:20:03 +0000130namespace {
131
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000132/// ARMOperand - Instances of this class represent a parsed ARM machine
133/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000134class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000135 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000136 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000137 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000138 CoprocNum,
139 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000140 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000141 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000142 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000143 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000144 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000145 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000146 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000147 DPRRegisterList,
148 SPRRegisterList,
Owen Anderson00828302011-03-18 22:50:18 +0000149 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000150 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000151 } Kind;
152
Sean Callanan76264762010-04-02 22:27:05 +0000153 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000154 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000155
156 union {
157 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000158 ARMCC::CondCodes Val;
159 } CC;
160
161 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000162 ARM_MB::MemBOpt Val;
163 } MBOpt;
164
165 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000166 unsigned Val;
167 } Cop;
168
169 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000170 ARM_PROC::IFlags Val;
171 } IFlags;
172
173 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000174 unsigned Val;
175 } MMask;
176
177 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000178 const char *Data;
179 unsigned Length;
180 } Tok;
181
182 struct {
183 unsigned RegNum;
184 } Reg;
185
Bill Wendling8155e5b2010-11-06 22:19:43 +0000186 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000187 const MCExpr *Val;
188 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000189
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000190 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000191 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000192 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000193 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000194 union {
195 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
196 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
197 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000198 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000199 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000200 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000201 unsigned Preindexed : 1;
202 unsigned Postindexed : 1;
203 unsigned OffsetIsReg : 1;
204 unsigned Negative : 1; // only used when OffsetIsReg is true
205 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000206 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000207
208 struct {
209 ARM_AM::ShiftOpc ShiftTy;
210 unsigned RegNum;
211 } Shift;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000212 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000213
Bill Wendling146018f2010-11-06 21:42:12 +0000214 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
215public:
Sean Callanan76264762010-04-02 22:27:05 +0000216 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
217 Kind = o.Kind;
218 StartLoc = o.StartLoc;
219 EndLoc = o.EndLoc;
220 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000221 case CondCode:
222 CC = o.CC;
223 break;
Sean Callanan76264762010-04-02 22:27:05 +0000224 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000225 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000226 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000227 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000228 case Register:
229 Reg = o.Reg;
230 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000231 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000232 case DPRRegisterList:
233 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000234 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000235 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000236 case CoprocNum:
237 case CoprocReg:
238 Cop = o.Cop;
239 break;
Sean Callanan76264762010-04-02 22:27:05 +0000240 case Immediate:
241 Imm = o.Imm;
242 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000243 case MemBarrierOpt:
244 MBOpt = o.MBOpt;
245 break;
Sean Callanan76264762010-04-02 22:27:05 +0000246 case Memory:
247 Mem = o.Mem;
248 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000249 case MSRMask:
250 MMask = o.MMask;
251 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000252 case ProcIFlags:
253 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000254 break;
255 case Shifter:
256 Shift = o.Shift;
257 break;
Sean Callanan76264762010-04-02 22:27:05 +0000258 }
259 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000260
Sean Callanan76264762010-04-02 22:27:05 +0000261 /// getStartLoc - Get the location of the first token of this operand.
262 SMLoc getStartLoc() const { return StartLoc; }
263 /// getEndLoc - Get the location of the last token of this operand.
264 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000265
Daniel Dunbar8462b302010-08-11 06:36:53 +0000266 ARMCC::CondCodes getCondCode() const {
267 assert(Kind == CondCode && "Invalid access!");
268 return CC.Val;
269 }
270
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000271 unsigned getCoproc() const {
272 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
273 return Cop.Val;
274 }
275
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000276 StringRef getToken() const {
277 assert(Kind == Token && "Invalid access!");
278 return StringRef(Tok.Data, Tok.Length);
279 }
280
281 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000282 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000283 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000284 }
285
Bill Wendling5fa22a12010-11-09 23:28:44 +0000286 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000287 assert((Kind == RegisterList || Kind == DPRRegisterList ||
288 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000289 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000290 }
291
Kevin Enderbycfe07242009-10-13 22:19:02 +0000292 const MCExpr *getImm() const {
293 assert(Kind == Immediate && "Invalid access!");
294 return Imm.Val;
295 }
296
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000297 ARM_MB::MemBOpt getMemBarrierOpt() const {
298 assert(Kind == MemBarrierOpt && "Invalid access!");
299 return MBOpt.Val;
300 }
301
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000302 ARM_PROC::IFlags getProcIFlags() const {
303 assert(Kind == ProcIFlags && "Invalid access!");
304 return IFlags.Val;
305 }
306
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000307 unsigned getMSRMask() const {
308 assert(Kind == MSRMask && "Invalid access!");
309 return MMask.Val;
310 }
311
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000312 /// @name Memory Operand Accessors
313 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000314 ARMII::AddrMode getMemAddrMode() const {
315 return Mem.AddrMode;
316 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000317 unsigned getMemBaseRegNum() const {
318 return Mem.BaseRegNum;
319 }
320 unsigned getMemOffsetRegNum() const {
321 assert(Mem.OffsetIsReg && "Invalid access!");
322 return Mem.Offset.RegNum;
323 }
324 const MCExpr *getMemOffset() const {
325 assert(!Mem.OffsetIsReg && "Invalid access!");
326 return Mem.Offset.Value;
327 }
328 unsigned getMemOffsetRegShifted() const {
329 assert(Mem.OffsetIsReg && "Invalid access!");
330 return Mem.OffsetRegShifted;
331 }
332 const MCExpr *getMemShiftAmount() const {
333 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
334 return Mem.ShiftAmount;
335 }
Owen Anderson00828302011-03-18 22:50:18 +0000336 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000337 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
338 return Mem.ShiftType;
339 }
340 bool getMemPreindexed() const { return Mem.Preindexed; }
341 bool getMemPostindexed() const { return Mem.Postindexed; }
342 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
343 bool getMemNegative() const { return Mem.Negative; }
344 bool getMemWriteback() const { return Mem.Writeback; }
345
346 /// @}
347
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000348 bool isCoprocNum() const { return Kind == CoprocNum; }
349 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000350 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000351 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000352 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000353 bool isImm0_255() const {
354 if (Kind != Immediate)
355 return false;
356 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
357 if (!CE) return false;
358 int64_t Value = CE->getValue();
359 return Value >= 0 && Value < 256;
360 }
361 bool isT2SOImm() const {
362 if (Kind != Immediate)
363 return false;
364 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
365 if (!CE) return false;
366 int64_t Value = CE->getValue();
367 return ARM_AM::getT2SOImmVal(Value) != -1;
368 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000369 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000370 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000371 bool isDPRRegList() const { return Kind == DPRRegisterList; }
372 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000373 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000374 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000375 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000376 bool isShifter() const { return Kind == Shifter; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000377 bool isMemMode2() const {
378 if (getMemAddrMode() != ARMII::AddrMode2)
379 return false;
380
381 if (getMemOffsetIsReg())
382 return true;
383
384 if (getMemNegative() &&
385 !(getMemPostindexed() || getMemPreindexed()))
386 return false;
387
388 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
389 if (!CE) return false;
390 int64_t Value = CE->getValue();
391
392 // The offset must be in the range 0-4095 (imm12).
393 if (Value > 4095 || Value < -4095)
394 return false;
395
396 return true;
397 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000398 bool isMemMode3() const {
399 if (getMemAddrMode() != ARMII::AddrMode3)
400 return false;
401
402 if (getMemOffsetIsReg()) {
403 if (getMemOffsetRegShifted())
404 return false; // No shift with offset reg allowed
405 return true;
406 }
407
408 if (getMemNegative() &&
409 !(getMemPostindexed() || getMemPreindexed()))
410 return false;
411
412 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
413 if (!CE) return false;
414 int64_t Value = CE->getValue();
415
416 // The offset must be in the range 0-255 (imm8).
417 if (Value > 255 || Value < -255)
418 return false;
419
420 return true;
421 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000422 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000423 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
424 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000425 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000426
Daniel Dunbar4b462672011-01-18 05:55:27 +0000427 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000428 if (!CE) return false;
429
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000430 // The offset must be a multiple of 4 in the range 0-1020.
431 int64_t Value = CE->getValue();
432 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
433 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000434 bool isMemMode7() const {
435 if (!isMemory() ||
436 getMemPreindexed() ||
437 getMemPostindexed() ||
438 getMemOffsetIsReg() ||
439 getMemNegative() ||
440 getMemWriteback())
441 return false;
442
443 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
444 if (!CE) return false;
445
446 if (CE->getValue())
447 return false;
448
449 return true;
450 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000451 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000452 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000453 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000454 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000455 }
456 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000457 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000458 return false;
459
Daniel Dunbar4b462672011-01-18 05:55:27 +0000460 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000461 if (!CE) return false;
462
463 // The offset must be a multiple of 4 in the range 0-124.
464 uint64_t Value = CE->getValue();
465 return ((Value & 0x3) == 0 && Value <= 124);
466 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000467 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000468 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000469
470 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000471 // Add as immediates when possible. Null MCExpr = 0.
472 if (Expr == 0)
473 Inst.addOperand(MCOperand::CreateImm(0));
474 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000475 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
476 else
477 Inst.addOperand(MCOperand::CreateExpr(Expr));
478 }
479
Daniel Dunbar8462b302010-08-11 06:36:53 +0000480 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000481 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000482 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000483 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
484 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000485 }
486
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000487 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
488 assert(N == 1 && "Invalid number of operands!");
489 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
490 }
491
492 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
493 assert(N == 1 && "Invalid number of operands!");
494 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
495 }
496
Jim Grosbachd67641b2010-12-06 18:21:12 +0000497 void addCCOutOperands(MCInst &Inst, unsigned N) const {
498 assert(N == 1 && "Invalid number of operands!");
499 Inst.addOperand(MCOperand::CreateReg(getReg()));
500 }
501
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000502 void addRegOperands(MCInst &Inst, unsigned N) const {
503 assert(N == 1 && "Invalid number of operands!");
504 Inst.addOperand(MCOperand::CreateReg(getReg()));
505 }
506
Owen Anderson00828302011-03-18 22:50:18 +0000507 void addShifterOperands(MCInst &Inst, unsigned N) const {
508 assert(N == 1 && "Invalid number of operands!");
509 Inst.addOperand(MCOperand::CreateImm(
510 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
511 }
512
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000513 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000514 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000515 const SmallVectorImpl<unsigned> &RegList = getRegList();
516 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000517 I = RegList.begin(), E = RegList.end(); I != E; ++I)
518 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000519 }
520
Bill Wendling0f630752010-11-17 04:32:08 +0000521 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
522 addRegListOperands(Inst, N);
523 }
524
525 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
526 addRegListOperands(Inst, N);
527 }
528
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000529 void addImmOperands(MCInst &Inst, unsigned N) const {
530 assert(N == 1 && "Invalid number of operands!");
531 addExpr(Inst, getImm());
532 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000533
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000534 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
535 assert(N == 1 && "Invalid number of operands!");
536 addExpr(Inst, getImm());
537 }
538
539 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
540 assert(N == 1 && "Invalid number of operands!");
541 addExpr(Inst, getImm());
542 }
543
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000544 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
545 assert(N == 1 && "Invalid number of operands!");
546 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
547 }
548
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000549 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
550 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
551 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
552
553 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000554 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000555 assert((CE || CE->getValue() == 0) &&
556 "No offset operand support in mode 7");
557 }
558
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000559 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
560 assert(isMemMode2() && "Invalid mode or number of operands!");
561 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
562 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
563
564 if (getMemOffsetIsReg()) {
565 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
566
567 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
568 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
569 int64_t ShiftAmount = 0;
570
571 if (getMemOffsetRegShifted()) {
572 ShOpc = getMemShiftType();
573 const MCConstantExpr *CE =
574 dyn_cast<MCConstantExpr>(getMemShiftAmount());
575 ShiftAmount = CE->getValue();
576 }
577
578 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
579 ShOpc, IdxMode)));
580 return;
581 }
582
583 // Create a operand placeholder to always yield the same number of operands.
584 Inst.addOperand(MCOperand::CreateReg(0));
585
586 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
587 // the difference?
588 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
589 assert(CE && "Non-constant mode 2 offset operand!");
590 int64_t Offset = CE->getValue();
591
592 if (Offset >= 0)
593 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
594 Offset, ARM_AM::no_shift, IdxMode)));
595 else
596 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
597 -Offset, ARM_AM::no_shift, IdxMode)));
598 }
599
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000600 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
601 assert(isMemMode3() && "Invalid mode or number of operands!");
602 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
603 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
604
605 if (getMemOffsetIsReg()) {
606 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
607
608 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
609 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
610 IdxMode)));
611 return;
612 }
613
614 // Create a operand placeholder to always yield the same number of operands.
615 Inst.addOperand(MCOperand::CreateReg(0));
616
617 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
618 // the difference?
619 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
620 assert(CE && "Non-constant mode 3 offset operand!");
621 int64_t Offset = CE->getValue();
622
623 if (Offset >= 0)
624 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
625 Offset, IdxMode)));
626 else
627 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
628 -Offset, IdxMode)));
629 }
630
Chris Lattner14b93852010-10-29 00:27:31 +0000631 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
632 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000633
Daniel Dunbar4b462672011-01-18 05:55:27 +0000634 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
635 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000636
Jim Grosbach80eb2332010-10-29 17:41:25 +0000637 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
638 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000639 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000640 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000641
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000642 // The MCInst offset operand doesn't include the low two bits (like
643 // the instruction encoding).
644 int64_t Offset = CE->getValue() / 4;
645 if (Offset >= 0)
646 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
647 Offset)));
648 else
649 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
650 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000651 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000652
Bill Wendlingf4caf692010-12-14 03:36:38 +0000653 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
654 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000655 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
656 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000657 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000658
Bill Wendlingf4caf692010-12-14 03:36:38 +0000659 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
660 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000661 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
662 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000663 assert(CE && "Non-constant mode offset operand!");
664 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000665 }
666
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000667 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
668 assert(N == 1 && "Invalid number of operands!");
669 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
670 }
671
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000672 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
673 assert(N == 1 && "Invalid number of operands!");
674 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
675 }
676
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000677 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000678
Chris Lattner3a697562010-10-28 17:20:03 +0000679 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
680 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000681 Op->CC.Val = CC;
682 Op->StartLoc = S;
683 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000684 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000685 }
686
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000687 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
688 ARMOperand *Op = new ARMOperand(CoprocNum);
689 Op->Cop.Val = CopVal;
690 Op->StartLoc = S;
691 Op->EndLoc = S;
692 return Op;
693 }
694
695 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
696 ARMOperand *Op = new ARMOperand(CoprocReg);
697 Op->Cop.Val = CopVal;
698 Op->StartLoc = S;
699 Op->EndLoc = S;
700 return Op;
701 }
702
Jim Grosbachd67641b2010-12-06 18:21:12 +0000703 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
704 ARMOperand *Op = new ARMOperand(CCOut);
705 Op->Reg.RegNum = RegNum;
706 Op->StartLoc = S;
707 Op->EndLoc = S;
708 return Op;
709 }
710
Chris Lattner3a697562010-10-28 17:20:03 +0000711 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
712 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000713 Op->Tok.Data = Str.data();
714 Op->Tok.Length = Str.size();
715 Op->StartLoc = S;
716 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000717 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000718 }
719
Bill Wendling50d0f582010-11-18 23:43:05 +0000720 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000721 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000722 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000723 Op->StartLoc = S;
724 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000725 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000726 }
727
Owen Anderson00828302011-03-18 22:50:18 +0000728 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
729 SMLoc S, SMLoc E) {
730 ARMOperand *Op = new ARMOperand(Shifter);
731 Op->Shift.ShiftTy = ShTy;
732 Op->StartLoc = S;
733 Op->EndLoc = E;
734 return Op;
735 }
736
Bill Wendling7729e062010-11-09 22:44:22 +0000737 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000738 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000739 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000740 KindTy Kind = RegisterList;
741
742 if (ARM::DPRRegClass.contains(Regs.front().first))
743 Kind = DPRRegisterList;
744 else if (ARM::SPRRegClass.contains(Regs.front().first))
745 Kind = SPRRegisterList;
746
747 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000748 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000749 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000750 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000751 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000752 Op->StartLoc = StartLoc;
753 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000754 return Op;
755 }
756
Chris Lattner3a697562010-10-28 17:20:03 +0000757 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
758 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000759 Op->Imm.Val = Val;
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 Enderbycfe07242009-10-13 22:19:02 +0000763 }
764
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000765 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
766 bool OffsetIsReg, const MCExpr *Offset,
767 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000768 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000769 const MCExpr *ShiftAmount, bool Preindexed,
770 bool Postindexed, bool Negative, bool Writeback,
771 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000772 assert((OffsetRegNum == -1 || OffsetIsReg) &&
773 "OffsetRegNum must imply OffsetIsReg!");
774 assert((!OffsetRegShifted || OffsetIsReg) &&
775 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000776 assert((Offset || OffsetIsReg) &&
777 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000778 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
779 "Cannot have shift amount without shifted register offset!");
780 assert((!Offset || !OffsetIsReg) &&
781 "Cannot have expression offset and register offset!");
782
Chris Lattner3a697562010-10-28 17:20:03 +0000783 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000784 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000785 Op->Mem.BaseRegNum = BaseRegNum;
786 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000787 if (OffsetIsReg)
788 Op->Mem.Offset.RegNum = OffsetRegNum;
789 else
790 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000791 Op->Mem.OffsetRegShifted = OffsetRegShifted;
792 Op->Mem.ShiftType = ShiftType;
793 Op->Mem.ShiftAmount = ShiftAmount;
794 Op->Mem.Preindexed = Preindexed;
795 Op->Mem.Postindexed = Postindexed;
796 Op->Mem.Negative = Negative;
797 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000798
Sean Callanan76264762010-04-02 22:27:05 +0000799 Op->StartLoc = S;
800 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000801 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000802 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000803
804 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
805 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
806 Op->MBOpt.Val = Opt;
807 Op->StartLoc = S;
808 Op->EndLoc = S;
809 return Op;
810 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000811
812 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
813 ARMOperand *Op = new ARMOperand(ProcIFlags);
814 Op->IFlags.Val = IFlags;
815 Op->StartLoc = S;
816 Op->EndLoc = S;
817 return Op;
818 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000819
820 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
821 ARMOperand *Op = new ARMOperand(MSRMask);
822 Op->MMask.Val = MMask;
823 Op->StartLoc = S;
824 Op->EndLoc = S;
825 return Op;
826 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000827};
828
829} // end anonymous namespace.
830
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000831void ARMOperand::dump(raw_ostream &OS) const {
832 switch (Kind) {
833 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000834 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000835 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000836 case CCOut:
837 OS << "<ccout " << getReg() << ">";
838 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000839 case CoprocNum:
840 OS << "<coprocessor number: " << getCoproc() << ">";
841 break;
842 case CoprocReg:
843 OS << "<coprocessor register: " << getCoproc() << ">";
844 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000845 case MSRMask:
846 OS << "<mask: " << getMSRMask() << ">";
847 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000848 case Immediate:
849 getImm()->print(OS);
850 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000851 case MemBarrierOpt:
852 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
853 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000854 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000855 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000856 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
857 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000858 if (getMemOffsetIsReg()) {
859 OS << " offset:<register " << getMemOffsetRegNum();
860 if (getMemOffsetRegShifted()) {
861 OS << " offset-shift-type:" << getMemShiftType();
862 OS << " offset-shift-amount:" << *getMemShiftAmount();
863 }
864 } else {
865 OS << " offset:" << *getMemOffset();
866 }
867 if (getMemOffsetIsReg())
868 OS << " (offset-is-reg)";
869 if (getMemPreindexed())
870 OS << " (pre-indexed)";
871 if (getMemPostindexed())
872 OS << " (post-indexed)";
873 if (getMemNegative())
874 OS << " (negative)";
875 if (getMemWriteback())
876 OS << " (writeback)";
877 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000878 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000879 case ProcIFlags: {
880 OS << "<ARM_PROC::";
881 unsigned IFlags = getProcIFlags();
882 for (int i=2; i >= 0; --i)
883 if (IFlags & (1 << i))
884 OS << ARM_PROC::IFlagsToString(1 << i);
885 OS << ">";
886 break;
887 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000888 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000889 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000890 break;
Owen Anderson00828302011-03-18 22:50:18 +0000891 case Shifter:
892 OS << "<shifter " << getShiftOpcStr(Shift.ShiftTy) << ">";
893 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000894 case RegisterList:
895 case DPRRegisterList:
896 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000897 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000898
Bill Wendling5fa22a12010-11-09 23:28:44 +0000899 const SmallVectorImpl<unsigned> &RegList = getRegList();
900 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000901 I = RegList.begin(), E = RegList.end(); I != E; ) {
902 OS << *I;
903 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000904 }
905
906 OS << ">";
907 break;
908 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000909 case Token:
910 OS << "'" << getToken() << "'";
911 break;
912 }
913}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000914
915/// @name Auto-generated Match Functions
916/// {
917
918static unsigned MatchRegisterName(StringRef Name);
919
920/// }
921
Bob Wilson69df7232011-02-03 21:46:10 +0000922bool ARMAsmParser::ParseRegister(unsigned &RegNo,
923 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000924 RegNo = TryParseRegister();
925
926 return (RegNo == (unsigned)-1);
927}
928
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000929/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000930/// and if it is a register name the token is eaten and the register number is
931/// returned. Otherwise return -1.
932///
933int ARMAsmParser::TryParseRegister() {
934 const AsmToken &Tok = Parser.getTok();
935 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000936
Chris Lattnere5658fa2010-10-30 04:09:10 +0000937 // FIXME: Validate register for the current architecture; we have to do
938 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000939 std::string upperCase = Tok.getString().str();
940 std::string lowerCase = LowercaseString(upperCase);
941 unsigned RegNum = MatchRegisterName(lowerCase);
942 if (!RegNum) {
943 RegNum = StringSwitch<unsigned>(lowerCase)
944 .Case("r13", ARM::SP)
945 .Case("r14", ARM::LR)
946 .Case("r15", ARM::PC)
947 .Case("ip", ARM::R12)
948 .Default(0);
949 }
950 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000951
Chris Lattnere5658fa2010-10-30 04:09:10 +0000952 Parser.Lex(); // Eat identifier token.
953 return RegNum;
954}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000955
Owen Anderson00828302011-03-18 22:50:18 +0000956/// Try to parse a register name. The token must be an Identifier when called,
957/// and if it is a register name the token is eaten and the register number is
958/// returned. Otherwise return -1.
959///
960bool ARMAsmParser::TryParseShiftRegister(
961 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
962 SMLoc S = Parser.getTok().getLoc();
963 const AsmToken &Tok = Parser.getTok();
964 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
965
966 std::string upperCase = Tok.getString().str();
967 std::string lowerCase = LowercaseString(upperCase);
968 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
969 .Case("lsl", ARM_AM::lsl)
970 .Case("lsr", ARM_AM::lsr)
971 .Case("asr", ARM_AM::asr)
972 .Case("ror", ARM_AM::ror)
973 .Case("rrx", ARM_AM::rrx)
974 .Default(ARM_AM::no_shift);
975
976 if (ShiftTy == ARM_AM::no_shift)
977 return true;
978
979 Parser.Lex(); // Eat shift-type operand;
980 int RegNum = TryParseRegister();
981 if (RegNum == -1)
982 return Error(Parser.getTok().getLoc(), "register expected");
983
984 Operands.push_back(ARMOperand::CreateReg(RegNum,S, Parser.getTok().getLoc()));
985 Operands.push_back(ARMOperand::CreateShifter(ShiftTy,
986 S, Parser.getTok().getLoc()));
987
988 return false;
989}
990
991
Bill Wendling50d0f582010-11-18 23:43:05 +0000992/// Try to parse a register name. The token must be an Identifier when called.
993/// If it's a register, an AsmOperand is created. Another AsmOperand is created
994/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000995///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000996/// TODO this is likely to change to allow different register types and or to
997/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000998bool ARMAsmParser::
999TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001000 SMLoc S = Parser.getTok().getLoc();
1001 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001002 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001003 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001004
Bill Wendling50d0f582010-11-18 23:43:05 +00001005 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001006
Chris Lattnere5658fa2010-10-30 04:09:10 +00001007 const AsmToken &ExclaimTok = Parser.getTok();
1008 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001009 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1010 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001011 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001012 }
1013
Bill Wendling50d0f582010-11-18 23:43:05 +00001014 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001015}
1016
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001017/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1018/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1019/// "c5", ...
1020static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001021 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1022 // but efficient.
1023 switch (Name.size()) {
1024 default: break;
1025 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001026 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001027 return -1;
1028 switch (Name[1]) {
1029 default: return -1;
1030 case '0': return 0;
1031 case '1': return 1;
1032 case '2': return 2;
1033 case '3': return 3;
1034 case '4': return 4;
1035 case '5': return 5;
1036 case '6': return 6;
1037 case '7': return 7;
1038 case '8': return 8;
1039 case '9': return 9;
1040 }
1041 break;
1042 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001043 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001044 return -1;
1045 switch (Name[2]) {
1046 default: return -1;
1047 case '0': return 10;
1048 case '1': return 11;
1049 case '2': return 12;
1050 case '3': return 13;
1051 case '4': return 14;
1052 case '5': return 15;
1053 }
1054 break;
1055 }
1056
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001057 return -1;
1058}
1059
Jim Grosbachf922c472011-02-12 01:34:40 +00001060/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001061/// token must be an Identifier when called, and if it is a coprocessor
1062/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001063ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1064tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001065 SMLoc S = Parser.getTok().getLoc();
1066 const AsmToken &Tok = Parser.getTok();
1067 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1068
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001069 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001070 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001071 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001072
1073 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001074 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001075 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001076}
1077
Jim Grosbachf922c472011-02-12 01:34:40 +00001078/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001079/// token must be an Identifier when called, and if it is a coprocessor
1080/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001081ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1082tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001083 SMLoc S = Parser.getTok().getLoc();
1084 const AsmToken &Tok = Parser.getTok();
1085 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1086
1087 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1088 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001089 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001090
1091 Parser.Lex(); // Eat identifier token.
1092 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001093 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001094}
1095
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001096/// Parse a register list, return it if successful else return null. The first
1097/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001098bool ARMAsmParser::
1099ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001100 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001101 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001102 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001103
Bill Wendling7729e062010-11-09 22:44:22 +00001104 // Read the rest of the registers in the list.
1105 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001106 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001107
Bill Wendling7729e062010-11-09 22:44:22 +00001108 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001109 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001110 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001111
Sean Callanan18b83232010-01-19 21:44:56 +00001112 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001113 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001114 if (RegTok.isNot(AsmToken::Identifier)) {
1115 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001116 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001117 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001118
Bill Wendling1d6a2652010-11-06 10:40:24 +00001119 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001120 if (RegNum == -1) {
1121 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001122 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001123 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001124
Bill Wendlinge7176102010-11-06 22:36:58 +00001125 if (IsRange) {
1126 int Reg = PrevRegNum;
1127 do {
1128 ++Reg;
1129 Registers.push_back(std::make_pair(Reg, RegLoc));
1130 } while (Reg != RegNum);
1131 } else {
1132 Registers.push_back(std::make_pair(RegNum, RegLoc));
1133 }
1134
1135 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001136 } while (Parser.getTok().is(AsmToken::Comma) ||
1137 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001138
1139 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001140 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001141 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1142 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001143 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001144 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001145
Bill Wendlinge7176102010-11-06 22:36:58 +00001146 SMLoc E = RCurlyTok.getLoc();
1147 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001148
Bill Wendlinge7176102010-11-06 22:36:58 +00001149 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001150 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001151 RI = Registers.begin(), RE = Registers.end();
1152
Bill Wendling7caebff2011-01-12 21:20:59 +00001153 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001154 bool EmittedWarning = false;
1155
Bill Wendling7caebff2011-01-12 21:20:59 +00001156 DenseMap<unsigned, bool> RegMap;
1157 RegMap[HighRegNum] = true;
1158
Bill Wendlinge7176102010-11-06 22:36:58 +00001159 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001160 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001161 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001162
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001163 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001164 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001165 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001166 }
1167
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001168 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001169 Warning(RegInfo.second,
1170 "register not in ascending order in register list");
1171
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001172 RegMap[Reg] = true;
1173 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001174 }
1175
Bill Wendling50d0f582010-11-18 23:43:05 +00001176 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1177 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001178}
1179
Jim Grosbachf922c472011-02-12 01:34:40 +00001180/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1181ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1182tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001183 SMLoc S = Parser.getTok().getLoc();
1184 const AsmToken &Tok = Parser.getTok();
1185 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1186 StringRef OptStr = Tok.getString();
1187
1188 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1189 .Case("sy", ARM_MB::SY)
1190 .Case("st", ARM_MB::ST)
1191 .Case("ish", ARM_MB::ISH)
1192 .Case("ishst", ARM_MB::ISHST)
1193 .Case("nsh", ARM_MB::NSH)
1194 .Case("nshst", ARM_MB::NSHST)
1195 .Case("osh", ARM_MB::OSH)
1196 .Case("oshst", ARM_MB::OSHST)
1197 .Default(~0U);
1198
1199 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001200 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001201
1202 Parser.Lex(); // Eat identifier token.
1203 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001204 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001205}
1206
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001207/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001208ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1209tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1210 SMLoc S = Parser.getTok().getLoc();
1211 const AsmToken &Tok = Parser.getTok();
1212 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1213 StringRef IFlagsStr = Tok.getString();
1214
1215 unsigned IFlags = 0;
1216 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1217 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1218 .Case("a", ARM_PROC::A)
1219 .Case("i", ARM_PROC::I)
1220 .Case("f", ARM_PROC::F)
1221 .Default(~0U);
1222
1223 // If some specific iflag is already set, it means that some letter is
1224 // present more than once, this is not acceptable.
1225 if (Flag == ~0U || (IFlags & Flag))
1226 return MatchOperand_NoMatch;
1227
1228 IFlags |= Flag;
1229 }
1230
1231 Parser.Lex(); // Eat identifier token.
1232 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1233 return MatchOperand_Success;
1234}
1235
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001236/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1237ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1238tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1239 SMLoc S = Parser.getTok().getLoc();
1240 const AsmToken &Tok = Parser.getTok();
1241 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1242 StringRef Mask = Tok.getString();
1243
1244 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1245 size_t Start = 0, Next = Mask.find('_');
1246 StringRef Flags = "";
1247 StringRef SpecReg = Mask.slice(Start, Next);
1248 if (Next != StringRef::npos)
1249 Flags = Mask.slice(Next+1, Mask.size());
1250
1251 // FlagsVal contains the complete mask:
1252 // 3-0: Mask
1253 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1254 unsigned FlagsVal = 0;
1255
1256 if (SpecReg == "apsr") {
1257 FlagsVal = StringSwitch<unsigned>(Flags)
1258 .Case("nzcvq", 0x8) // same as CPSR_c
1259 .Case("g", 0x4) // same as CPSR_s
1260 .Case("nzcvqg", 0xc) // same as CPSR_fs
1261 .Default(~0U);
1262
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001263 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001264 if (!Flags.empty())
1265 return MatchOperand_NoMatch;
1266 else
1267 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001268 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001269 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001270 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1271 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001272 for (int i = 0, e = Flags.size(); i != e; ++i) {
1273 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1274 .Case("c", 1)
1275 .Case("x", 2)
1276 .Case("s", 4)
1277 .Case("f", 8)
1278 .Default(~0U);
1279
1280 // If some specific flag is already set, it means that some letter is
1281 // present more than once, this is not acceptable.
1282 if (FlagsVal == ~0U || (FlagsVal & Flag))
1283 return MatchOperand_NoMatch;
1284 FlagsVal |= Flag;
1285 }
1286 } else // No match for special register.
1287 return MatchOperand_NoMatch;
1288
1289 // Special register without flags are equivalent to "fc" flags.
1290 if (!FlagsVal)
1291 FlagsVal = 0x9;
1292
1293 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1294 if (SpecReg == "spsr")
1295 FlagsVal |= 16;
1296
1297 Parser.Lex(); // Eat identifier token.
1298 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1299 return MatchOperand_Success;
1300}
1301
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001302/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1303ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1304tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001305 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001306
1307 if (ParseMemory(Operands, ARMII::AddrMode2))
1308 return MatchOperand_NoMatch;
1309
1310 return MatchOperand_Success;
1311}
1312
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001313/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1314ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1315tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1316 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1317
1318 if (ParseMemory(Operands, ARMII::AddrMode3))
1319 return MatchOperand_NoMatch;
1320
1321 return MatchOperand_Success;
1322}
1323
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001324/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1325/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1326/// when they refer multiple MIOperands inside a single one.
1327bool ARMAsmParser::
1328CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1329 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1330 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1331
1332 // Create a writeback register dummy placeholder.
1333 Inst.addOperand(MCOperand::CreateImm(0));
1334
1335 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1336 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1337 return true;
1338}
1339
1340/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1341/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1342/// when they refer multiple MIOperands inside a single one.
1343bool ARMAsmParser::
1344CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1345 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1346 // Create a writeback register dummy placeholder.
1347 Inst.addOperand(MCOperand::CreateImm(0));
1348 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1349 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1350 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1351 return true;
1352}
1353
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001354/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1355/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1356/// when they refer multiple MIOperands inside a single one.
1357bool ARMAsmParser::
1358CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1359 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1360 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1361
1362 // Create a writeback register dummy placeholder.
1363 Inst.addOperand(MCOperand::CreateImm(0));
1364
1365 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1366 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1367 return true;
1368}
1369
1370/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1371/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1372/// when they refer multiple MIOperands inside a single one.
1373bool ARMAsmParser::
1374CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1375 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1376 // Create a writeback register dummy placeholder.
1377 Inst.addOperand(MCOperand::CreateImm(0));
1378 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1379 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1380 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1381 return true;
1382}
1383
Bill Wendlinge7176102010-11-06 22:36:58 +00001384/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001385/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001386///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001387/// TODO Only preindexing and postindexing addressing are started, unindexed
1388/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001389bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001390ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1391 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001392 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001393 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001394 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001395 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001396 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001397
Sean Callanan18b83232010-01-19 21:44:56 +00001398 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001399 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1400 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001401 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001402 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001403 int BaseRegNum = TryParseRegister();
1404 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001405 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001406 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001407 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001408
Daniel Dunbar05710932011-01-18 05:34:17 +00001409 // The next token must either be a comma or a closing bracket.
1410 const AsmToken &Tok = Parser.getTok();
1411 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1412 return true;
1413
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001414 bool Preindexed = false;
1415 bool Postindexed = false;
1416 bool OffsetIsReg = false;
1417 bool Negative = false;
1418 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001419 ARMOperand *WBOp = 0;
1420 int OffsetRegNum = -1;
1421 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001422 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001423 const MCExpr *ShiftAmount = 0;
1424 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001425
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001426 // First look for preindexed address forms, that is after the "[Rn" we now
1427 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001428 if (Tok.is(AsmToken::Comma)) {
1429 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001430 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001431
Chris Lattner550276e2010-10-28 20:52:15 +00001432 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1433 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001434 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001435 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001436 if (RBracTok.isNot(AsmToken::RBrac)) {
1437 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001438 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001439 }
Sean Callanan76264762010-04-02 22:27:05 +00001440 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001441 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001442
Sean Callanan18b83232010-01-19 21:44:56 +00001443 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001444 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001445 // None of addrmode3 instruction uses "!"
1446 if (AddrMode == ARMII::AddrMode3)
1447 return true;
1448
Bill Wendling50d0f582010-11-18 23:43:05 +00001449 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1450 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001451 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001452 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001453 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1454 if (AddrMode == ARMII::AddrMode2)
1455 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001456 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001457 } else {
1458 // The "[Rn" we have so far was not followed by a comma.
1459
Jim Grosbach80eb2332010-10-29 17:41:25 +00001460 // If there's anything other than the right brace, this is a post indexing
1461 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001462 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001463 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001464
Sean Callanan18b83232010-01-19 21:44:56 +00001465 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001466
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001467 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001468 Postindexed = true;
1469 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001470
Chris Lattner550276e2010-10-28 20:52:15 +00001471 if (NextTok.isNot(AsmToken::Comma)) {
1472 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001473 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001474 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001475
Sean Callananb9a25b72010-01-19 20:27:46 +00001476 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001477
Chris Lattner550276e2010-10-28 20:52:15 +00001478 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001479 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001480 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001481 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001482 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001483 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001484
1485 // Force Offset to exist if used.
1486 if (!OffsetIsReg) {
1487 if (!Offset)
1488 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001489 } else {
1490 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1491 Error(E, "shift amount not supported");
1492 return true;
1493 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001494 }
1495
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001496 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1497 Offset, OffsetRegNum, OffsetRegShifted,
1498 ShiftType, ShiftAmount, Preindexed,
1499 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001500 if (WBOp)
1501 Operands.push_back(WBOp);
1502
1503 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001504}
1505
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001506/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1507/// we will parse the following (were +/- means that a plus or minus is
1508/// optional):
1509/// +/-Rm
1510/// +/-Rm, shift
1511/// #offset
1512/// we return false on success or an error otherwise.
1513bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001514 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001515 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001516 const MCExpr *&ShiftAmount,
1517 const MCExpr *&Offset,
1518 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001519 int &OffsetRegNum,
1520 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001521 Negative = false;
1522 OffsetRegShifted = false;
1523 OffsetIsReg = false;
1524 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001525 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001526 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001527 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001528 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001529 else if (NextTok.is(AsmToken::Minus)) {
1530 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001531 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001532 }
1533 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001534 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001535 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001536 SMLoc CurLoc = OffsetRegTok.getLoc();
1537 OffsetRegNum = TryParseRegister();
1538 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001539 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001540 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001541 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001542 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001543
Bill Wendling12f40e92010-11-06 10:51:53 +00001544 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001545 if (OffsetRegNum != -1) {
1546 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001547 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001548 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001549 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001550
Sean Callanan18b83232010-01-19 21:44:56 +00001551 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001552 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001553 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001554 OffsetRegShifted = true;
1555 }
1556 }
1557 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1558 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001559 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001560 if (HashTok.isNot(AsmToken::Hash))
1561 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001562
Sean Callananb9a25b72010-01-19 20:27:46 +00001563 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001564
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001565 if (getParser().ParseExpression(Offset))
1566 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001567 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001568 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001569 return false;
1570}
1571
1572/// ParseShift as one of these two:
1573/// ( lsl | lsr | asr | ror ) , # shift_amount
1574/// rrx
1575/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001576bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1577 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001578 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001579 if (Tok.isNot(AsmToken::Identifier))
1580 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001581 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001582 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001583 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001584 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001585 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001586 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001587 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001588 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001589 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001590 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001591 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001592 else
1593 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001594 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001595
1596 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001597 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001598 return false;
1599
1600 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001601 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001602 if (HashTok.isNot(AsmToken::Hash))
1603 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001604 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001605
1606 if (getParser().ParseExpression(ShiftAmount))
1607 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001608
1609 return false;
1610}
1611
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001612/// Parse a arm instruction operand. For now this parses the operand regardless
1613/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001614bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001615 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001616 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001617
1618 // Check if the current operand has a custom associated parser, if so, try to
1619 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001620 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1621 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001622 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001623 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1624 // there was a match, but an error occurred, in which case, just return that
1625 // the operand parsing failed.
1626 if (ResTy == MatchOperand_ParseFail)
1627 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001628
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001629 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001630 default:
1631 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001632 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001633 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001634 if (!TryParseRegisterWithWriteBack(Operands))
1635 return false;
Owen Anderson00828302011-03-18 22:50:18 +00001636 if (!TryParseShiftRegister(Operands))
1637 return false;
1638
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001639
1640 // Fall though for the Identifier case that is not a register or a
1641 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001642 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1643 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001644 // This was not a register so parse other operands that start with an
1645 // identifier (like labels) as expressions and create them as immediates.
1646 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001647 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001648 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001649 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001650 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001651 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1652 return false;
1653 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001654 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001655 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001656 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001657 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001658 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001659 // #42 -> immediate.
1660 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001661 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001662 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001663 const MCExpr *ImmVal;
1664 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001665 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001666 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001667 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1668 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001669 case AsmToken::Colon: {
1670 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001671 // FIXME: Check it's an expression prefix,
1672 // e.g. (FOO - :lower16:BAR) isn't legal.
1673 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001674 if (ParsePrefix(RefKind))
1675 return true;
1676
Evan Cheng75972122011-01-13 07:58:56 +00001677 const MCExpr *SubExprVal;
1678 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001679 return true;
1680
Evan Cheng75972122011-01-13 07:58:56 +00001681 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1682 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001683 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001684 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001685 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001686 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001687 }
1688}
1689
Evan Cheng75972122011-01-13 07:58:56 +00001690// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1691// :lower16: and :upper16:.
1692bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1693 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001694
1695 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001696 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001697 Parser.Lex(); // Eat ':'
1698
1699 if (getLexer().isNot(AsmToken::Identifier)) {
1700 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1701 return true;
1702 }
1703
1704 StringRef IDVal = Parser.getTok().getIdentifier();
1705 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001706 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001707 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001708 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001709 } else {
1710 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1711 return true;
1712 }
1713 Parser.Lex();
1714
1715 if (getLexer().isNot(AsmToken::Colon)) {
1716 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1717 return true;
1718 }
1719 Parser.Lex(); // Eat the last ':'
1720 return false;
1721}
1722
1723const MCExpr *
1724ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1725 MCSymbolRefExpr::VariantKind Variant) {
1726 // Recurse over the given expression, rebuilding it to apply the given variant
1727 // to the leftmost symbol.
1728 if (Variant == MCSymbolRefExpr::VK_None)
1729 return E;
1730
1731 switch (E->getKind()) {
1732 case MCExpr::Target:
1733 llvm_unreachable("Can't handle target expr yet");
1734 case MCExpr::Constant:
1735 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1736
1737 case MCExpr::SymbolRef: {
1738 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1739
1740 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1741 return 0;
1742
1743 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1744 }
1745
1746 case MCExpr::Unary:
1747 llvm_unreachable("Can't handle unary expressions yet");
1748
1749 case MCExpr::Binary: {
1750 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1751 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1752 const MCExpr *RHS = BE->getRHS();
1753 if (!LHS)
1754 return 0;
1755
1756 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1757 }
1758 }
1759
1760 assert(0 && "Invalid expression kind!");
1761 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001762}
1763
Daniel Dunbar352e1482011-01-11 15:59:50 +00001764/// \brief Given a mnemonic, split out possible predication code and carry
1765/// setting letters to form a canonical mnemonic and flags.
1766//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001767// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001768static StringRef SplitMnemonic(StringRef Mnemonic,
1769 unsigned &PredicationCode,
1770 bool &CarrySetting,
1771 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001772 PredicationCode = ARMCC::AL;
1773 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001774 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001775
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001776 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001777 //
1778 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001779 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1780 Mnemonic == "movs" ||
1781 Mnemonic == "svc" ||
1782 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1783 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1784 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1785 Mnemonic == "vclt" ||
1786 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1787 Mnemonic == "vcle" ||
1788 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1789 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbachd1f0bbe2011-06-27 20:59:10 +00001790 Mnemonic == "vqdmlal" || Mnemonic == "bics"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001791 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001792
Daniel Dunbar352e1482011-01-11 15:59:50 +00001793 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001794 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001795 .Case("eq", ARMCC::EQ)
1796 .Case("ne", ARMCC::NE)
1797 .Case("hs", ARMCC::HS)
Jim Grosbach660a9ec2011-06-27 20:40:29 +00001798 .Case("cs", ARMCC::HS)
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001799 .Case("lo", ARMCC::LO)
Jim Grosbach660a9ec2011-06-27 20:40:29 +00001800 .Case("cc", ARMCC::LO)
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001801 .Case("mi", ARMCC::MI)
1802 .Case("pl", ARMCC::PL)
1803 .Case("vs", ARMCC::VS)
1804 .Case("vc", ARMCC::VC)
1805 .Case("hi", ARMCC::HI)
1806 .Case("ls", ARMCC::LS)
1807 .Case("ge", ARMCC::GE)
1808 .Case("lt", ARMCC::LT)
1809 .Case("gt", ARMCC::GT)
1810 .Case("le", ARMCC::LE)
1811 .Case("al", ARMCC::AL)
1812 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001813 if (CC != ~0U) {
1814 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001815 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001816 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001817
Daniel Dunbar352e1482011-01-11 15:59:50 +00001818 // Next, determine if we have a carry setting bit. We explicitly ignore all
1819 // the instructions we know end in 's'.
1820 if (Mnemonic.endswith("s") &&
1821 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1822 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1823 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1824 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1825 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1826 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1827 CarrySetting = true;
1828 }
1829
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001830 // The "cps" instruction can have a interrupt mode operand which is glued into
1831 // the mnemonic. Check if this is the case, split it and parse the imod op
1832 if (Mnemonic.startswith("cps")) {
1833 // Split out any imod code.
1834 unsigned IMod =
1835 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1836 .Case("ie", ARM_PROC::IE)
1837 .Case("id", ARM_PROC::ID)
1838 .Default(~0U);
1839 if (IMod != ~0U) {
1840 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1841 ProcessorIMod = IMod;
1842 }
1843 }
1844
Daniel Dunbar352e1482011-01-11 15:59:50 +00001845 return Mnemonic;
1846}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001847
1848/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1849/// inclusion of carry set or predication code operands.
1850//
1851// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001852void ARMAsmParser::
1853GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1854 bool &CanAcceptPredicationCode) {
Jim Grosbachdf8fe992011-06-27 21:38:03 +00001855 bool isThumbOne = TM.getSubtarget<ARMSubtarget>().isThumb1Only();
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001856 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1857
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001858 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1859 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1860 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1861 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001862 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001863 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1864 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001865 Mnemonic == "eor" || Mnemonic == "smlal" ||
Jim Grosbachdf8fe992011-06-27 21:38:03 +00001866 (Mnemonic == "mov" && !isThumbOne)) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001867 CanAcceptCarrySet = true;
1868 } else {
1869 CanAcceptCarrySet = false;
1870 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001871
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001872 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1873 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1874 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1875 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001876 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001877 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001878 CanAcceptPredicationCode = false;
1879 } else {
1880 CanAcceptPredicationCode = true;
1881 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001882
1883 if (isThumb)
1884 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001885 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001886 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001887}
1888
1889/// Parse an arm instruction mnemonic followed by its operands.
1890bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1891 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1892 // Create the leading tokens for the mnemonic, split by '.' characters.
1893 size_t Start = 0, Next = Name.find('.');
1894 StringRef Head = Name.slice(Start, Next);
1895
Daniel Dunbar352e1482011-01-11 15:59:50 +00001896 // Split out the predication code and carry setting flag from the mnemonic.
1897 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001898 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001899 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001900 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1901 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001902
Chris Lattner3a697562010-10-28 17:20:03 +00001903 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001904
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001905 // Next, add the CCOut and ConditionCode operands, if needed.
1906 //
1907 // For mnemonics which can ever incorporate a carry setting bit or predication
1908 // code, our matching model involves us always generating CCOut and
1909 // ConditionCode operands to match the mnemonic "as written" and then we let
1910 // the matcher deal with finding the right instruction or generating an
1911 // appropriate error.
1912 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1913 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1914
1915 // Add the carry setting operand, if necessary.
1916 //
1917 // FIXME: It would be awesome if we could somehow invent a location such that
1918 // match errors on this operand would print a nice diagnostic about how the
1919 // 's' character in the mnemonic resulted in a CCOut operand.
1920 if (CanAcceptCarrySet) {
1921 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1922 NameLoc));
1923 } else {
1924 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1925 // misspelled another mnemonic).
1926
1927 // FIXME: Issue a nice error.
1928 }
1929
1930 // Add the predication code operand, if necessary.
1931 if (CanAcceptPredicationCode) {
1932 Operands.push_back(ARMOperand::CreateCondCode(
1933 ARMCC::CondCodes(PredicationCode), NameLoc));
1934 } else {
1935 // This mnemonic can't ever accept a predication code, but the user wrote
1936 // one (or misspelled another mnemonic).
1937
1938 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001939 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001940
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001941 // Add the processor imod operand, if necessary.
1942 if (ProcessorIMod) {
1943 Operands.push_back(ARMOperand::CreateImm(
1944 MCConstantExpr::Create(ProcessorIMod, getContext()),
1945 NameLoc, NameLoc));
1946 } else {
1947 // This mnemonic can't ever accept a imod, but the user wrote
1948 // one (or misspelled another mnemonic).
1949
1950 // FIXME: Issue a nice error.
1951 }
1952
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001953 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001954 while (Next != StringRef::npos) {
1955 Start = Next;
1956 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001957 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001958
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001959 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001960 }
1961
1962 // Read the remaining operands.
1963 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001964 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001965 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001966 Parser.EatToEndOfStatement();
1967 return true;
1968 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001969
1970 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001971 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001972
1973 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001974 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001975 Parser.EatToEndOfStatement();
1976 return true;
1977 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001978 }
1979 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001980
Chris Lattnercbf8a982010-09-11 16:18:25 +00001981 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1982 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001983 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001984 }
Bill Wendling146018f2010-11-06 21:42:12 +00001985
Chris Lattner34e53142010-09-08 05:10:46 +00001986 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001987 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001988}
1989
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001990bool ARMAsmParser::
1991MatchAndEmitInstruction(SMLoc IDLoc,
1992 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1993 MCStreamer &Out) {
1994 MCInst Inst;
1995 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001996 MatchResultTy MatchResult, MatchResult2;
1997 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1998 if (MatchResult != Match_Success) {
1999 // If we get a Match_InvalidOperand it might be some arithmetic instruction
2000 // that does not update the condition codes. So try adding a CCOut operand
2001 // with a value of reg0.
2002 if (MatchResult == Match_InvalidOperand) {
2003 Operands.insert(Operands.begin() + 1,
2004 ARMOperand::CreateCCOut(0,
2005 ((ARMOperand*)Operands[0])->getStartLoc()));
2006 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2007 if (MatchResult2 == Match_Success)
2008 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002009 else {
2010 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002011 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002012 delete CCOut;
2013 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002014 }
2015 // If we get a Match_MnemonicFail it might be some arithmetic instruction
2016 // that updates the condition codes if it ends in 's'. So see if the
2017 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
2018 // operand with a value of CPSR.
2019 else if(MatchResult == Match_MnemonicFail) {
2020 // Get the instruction mnemonic, which is the first token.
2021 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
2022 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
2023 // removed the 's' from the mnemonic for matching.
2024 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
2025 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002026 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2027 Operands.erase(Operands.begin());
2028 delete OldMnemonic;
2029 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002030 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
2031 Operands.insert(Operands.begin() + 1,
2032 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2033 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2034 if (MatchResult2 == Match_Success)
2035 MatchResult = Match_Success;
2036 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002037 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2038 Operands.erase(Operands.begin());
2039 delete OldMnemonic;
2040 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002041 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002042 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2043 Operands.erase(Operands.begin() + 1);
2044 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002045 }
2046 }
2047 }
2048 }
2049 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002050 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002051 Out.EmitInstruction(Inst);
2052 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002053 case Match_MissingFeature:
2054 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2055 return true;
2056 case Match_InvalidOperand: {
2057 SMLoc ErrorLoc = IDLoc;
2058 if (ErrorInfo != ~0U) {
2059 if (ErrorInfo >= Operands.size())
2060 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002061
Chris Lattnere73d4f82010-10-28 21:41:58 +00002062 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2063 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2064 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002065
Chris Lattnere73d4f82010-10-28 21:41:58 +00002066 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002067 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002068 case Match_MnemonicFail:
2069 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002070 case Match_ConversionFail:
2071 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002072 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002073
Eric Christopherc223e2b2010-10-29 09:26:59 +00002074 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002075 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002076}
2077
Kevin Enderby515d5092009-10-15 20:48:48 +00002078/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002079bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2080 StringRef IDVal = DirectiveID.getIdentifier();
2081 if (IDVal == ".word")
2082 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002083 else if (IDVal == ".thumb")
2084 return ParseDirectiveThumb(DirectiveID.getLoc());
2085 else if (IDVal == ".thumb_func")
2086 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2087 else if (IDVal == ".code")
2088 return ParseDirectiveCode(DirectiveID.getLoc());
2089 else if (IDVal == ".syntax")
2090 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002091 return true;
2092}
2093
2094/// ParseDirectiveWord
2095/// ::= .word [ expression (, expression)* ]
2096bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2097 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2098 for (;;) {
2099 const MCExpr *Value;
2100 if (getParser().ParseExpression(Value))
2101 return true;
2102
Chris Lattneraaec2052010-01-19 19:46:13 +00002103 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002104
2105 if (getLexer().is(AsmToken::EndOfStatement))
2106 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002107
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002108 // FIXME: Improve diagnostic.
2109 if (getLexer().isNot(AsmToken::Comma))
2110 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002111 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002112 }
2113 }
2114
Sean Callananb9a25b72010-01-19 20:27:46 +00002115 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002116 return false;
2117}
2118
Kevin Enderby515d5092009-10-15 20:48:48 +00002119/// ParseDirectiveThumb
2120/// ::= .thumb
2121bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2122 if (getLexer().isNot(AsmToken::EndOfStatement))
2123 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002124 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002125
2126 // TODO: set thumb mode
2127 // TODO: tell the MC streamer the mode
2128 // getParser().getStreamer().Emit???();
2129 return false;
2130}
2131
2132/// ParseDirectiveThumbFunc
2133/// ::= .thumbfunc symbol_name
2134bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002135 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2136 bool isMachO = MAI.hasSubsectionsViaSymbols();
2137 StringRef Name;
2138
2139 // Darwin asm has function name after .thumb_func direction
2140 // ELF doesn't
2141 if (isMachO) {
2142 const AsmToken &Tok = Parser.getTok();
2143 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2144 return Error(L, "unexpected token in .thumb_func directive");
2145 Name = Tok.getString();
2146 Parser.Lex(); // Consume the identifier token.
2147 }
2148
Kevin Enderby515d5092009-10-15 20:48:48 +00002149 if (getLexer().isNot(AsmToken::EndOfStatement))
2150 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002151 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002152
Rafael Espindola64695402011-05-16 16:17:21 +00002153 // FIXME: assuming function name will be the line following .thumb_func
2154 if (!isMachO) {
2155 Name = Parser.getTok().getString();
2156 }
2157
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002158 // Mark symbol as a thumb symbol.
2159 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2160 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002161 return false;
2162}
2163
2164/// ParseDirectiveSyntax
2165/// ::= .syntax unified | divided
2166bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002167 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002168 if (Tok.isNot(AsmToken::Identifier))
2169 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002170 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002171 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002172 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002173 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002174 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002175 else
2176 return Error(L, "unrecognized syntax mode in .syntax directive");
2177
2178 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002179 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002180 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002181
2182 // TODO tell the MC streamer the mode
2183 // getParser().getStreamer().Emit???();
2184 return false;
2185}
2186
2187/// ParseDirectiveCode
2188/// ::= .code 16 | 32
2189bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002190 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002191 if (Tok.isNot(AsmToken::Integer))
2192 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002193 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002194 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002195 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002196 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002197 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002198 else
2199 return Error(L, "invalid operand to .code directive");
2200
2201 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002202 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002203 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002204
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002205 // FIXME: We need to be able switch subtargets at this point so that
2206 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
2207 // includes Feature_IsThumb or not to match the right instructions. This is
2208 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
2209 if (Val == 16){
2210 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
2211 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002212 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002213 }
2214 else{
2215 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
2216 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002217 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002218 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002219
Kevin Enderby515d5092009-10-15 20:48:48 +00002220 return false;
2221}
2222
Sean Callanan90b70972010-04-07 20:29:34 +00002223extern "C" void LLVMInitializeARMAsmLexer();
2224
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002225/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002226extern "C" void LLVMInitializeARMAsmParser() {
2227 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2228 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002229 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002230}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002231
Chris Lattner0692ee62010-09-06 19:11:01 +00002232#define GET_REGISTER_MATCHER
2233#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002234#include "ARMGenAsmMatcher.inc"