blob: b3bf725ed56918a893d33754cfd1d213b6bce99d [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ARM.h"
Bill Wendling92b5a2e2010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Evan Cheng75972122011-01-13 07:58:56 +000012#include "ARMMCExpr.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000013#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000014#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
17#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Rafael Espindola64695402011-05-16 16:17:21 +000018#include "llvm/MC/MCAsmInfo.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000019#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000020#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000023#include "llvm/MC/MCSubtargetInfo.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000024#include "llvm/Target/TargetRegistry.h"
25#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000027#include "llvm/Support/raw_ostream.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000028#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000030#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000031#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000032#include "llvm/ADT/Twine.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000033
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000034using namespace llvm;
35
Chris Lattner3a697562010-10-28 17:20:03 +000036namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000037
38class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000039
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000040class ARMAsmParser : public TargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000041 MCSubtargetInfo &STI;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042 MCAsmParser &Parser;
43
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000044 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
46
47 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
49
Chris Lattnere5658fa2010-10-30 04:09:10 +000050 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000051 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000052 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach19906722011-07-13 18:49:30 +000053 int TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000054 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000055 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
56 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000057 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000058 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000059 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
60 MCSymbolRefExpr::VariantKind Variant);
61
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000062
Kevin Enderby9c41fa82009-10-30 22:55:57 +000063 bool ParseMemoryOffsetReg(bool &Negative,
64 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000065 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 const MCExpr *&ShiftAmount,
67 const MCExpr *&Offset,
68 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000069 int &OffsetRegNum,
70 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000071 bool ParseShift(enum ARM_AM::ShiftOpc &St,
72 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000073 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000074 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000075 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000076 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveSyntax(SMLoc L);
78
Chris Lattner7036f8b2010-09-29 01:42:58 +000079 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000080 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000081 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000082 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
83 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000084
Evan Chengebdeeab2011-07-08 01:53:10 +000085 bool isThumb() const {
86 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000087 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000088 }
Evan Chengebdeeab2011-07-08 01:53:10 +000089 bool isThumbOne() const {
Evan Chengffc0e732011-07-09 05:47:46 +000090 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000091 }
Evan Cheng32869202011-07-08 22:36:29 +000092 void SwitchMode() {
Evan Chengffc0e732011-07-09 05:47:46 +000093 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
94 setAvailableFeatures(FB);
Evan Cheng32869202011-07-08 22:36:29 +000095 }
Evan Chengebdeeab2011-07-08 01:53:10 +000096
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000097 /// @name Auto-generated Match Functions
98 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000099
Chris Lattner0692ee62010-09-06 19:11:01 +0000100#define GET_ASSEMBLER_HEADER
101#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000102
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000103 /// }
104
Jim Grosbachf922c472011-02-12 01:34:40 +0000105 OperandMatchResultTy tryParseCoprocNumOperand(
106 SmallVectorImpl<MCParsedAsmOperand*>&);
107 OperandMatchResultTy tryParseCoprocRegOperand(
108 SmallVectorImpl<MCParsedAsmOperand*>&);
109 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000110 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000111 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000112 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000113 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000114 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000115 OperandMatchResultTy tryParseMemMode2Operand(
116 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000117 OperandMatchResultTy tryParseMemMode3Operand(
118 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000119
120 // Asm Match Converter Methods
121 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
122 const SmallVectorImpl<MCParsedAsmOperand*> &);
123 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
124 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000125 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
126 const SmallVectorImpl<MCParsedAsmOperand*> &);
127 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
128 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000129
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000130public:
Evan Chengffc0e732011-07-09 05:47:46 +0000131 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
132 : TargetAsmParser(), STI(_STI), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000133 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000134
Evan Chengebdeeab2011-07-08 01:53:10 +0000135 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000136 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evan Chengebdeeab2011-07-08 01:53:10 +0000137 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000138
Benjamin Kramer38e59892010-07-14 22:38:02 +0000139 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000140 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000141 virtual bool ParseDirective(AsmToken DirectiveID);
142};
Jim Grosbach16c74252010-10-29 14:46:02 +0000143} // end anonymous namespace
144
Chris Lattner3a697562010-10-28 17:20:03 +0000145namespace {
146
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000147/// ARMOperand - Instances of this class represent a parsed ARM machine
148/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000149class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000150 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000151 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000152 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000153 CoprocNum,
154 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000155 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000156 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000157 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000158 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000159 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000160 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000161 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000162 DPRRegisterList,
163 SPRRegisterList,
Jim Grosbache8606dc2011-07-13 17:50:29 +0000164 ShiftedRegister,
Owen Anderson00828302011-03-18 22:50:18 +0000165 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000166 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000167 } Kind;
168
Sean Callanan76264762010-04-02 22:27:05 +0000169 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000170 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000171
172 union {
173 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000174 ARMCC::CondCodes Val;
175 } CC;
176
177 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000178 ARM_MB::MemBOpt Val;
179 } MBOpt;
180
181 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000182 unsigned Val;
183 } Cop;
184
185 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000186 ARM_PROC::IFlags Val;
187 } IFlags;
188
189 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000190 unsigned Val;
191 } MMask;
192
193 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000194 const char *Data;
195 unsigned Length;
196 } Tok;
197
198 struct {
199 unsigned RegNum;
200 } Reg;
201
Bill Wendling8155e5b2010-11-06 22:19:43 +0000202 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000203 const MCExpr *Val;
204 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000205
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000206 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000207 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000208 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000209 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000210 union {
211 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
212 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
213 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000214 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000215 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000216 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000217 unsigned Preindexed : 1;
218 unsigned Postindexed : 1;
219 unsigned OffsetIsReg : 1;
220 unsigned Negative : 1; // only used when OffsetIsReg is true
221 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000222 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000223
224 struct {
225 ARM_AM::ShiftOpc ShiftTy;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000226 unsigned Imm;
Owen Anderson00828302011-03-18 22:50:18 +0000227 } Shift;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000228 struct {
229 ARM_AM::ShiftOpc ShiftTy;
230 unsigned SrcReg;
231 unsigned ShiftReg;
232 unsigned ShiftImm;
233 } ShiftedReg;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000234 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000235
Bill Wendling146018f2010-11-06 21:42:12 +0000236 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
237public:
Sean Callanan76264762010-04-02 22:27:05 +0000238 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
239 Kind = o.Kind;
240 StartLoc = o.StartLoc;
241 EndLoc = o.EndLoc;
242 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000243 case CondCode:
244 CC = o.CC;
245 break;
Sean Callanan76264762010-04-02 22:27:05 +0000246 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000247 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000248 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000249 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000250 case Register:
251 Reg = o.Reg;
252 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000253 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000254 case DPRRegisterList:
255 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000256 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000257 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000258 case CoprocNum:
259 case CoprocReg:
260 Cop = o.Cop;
261 break;
Sean Callanan76264762010-04-02 22:27:05 +0000262 case Immediate:
263 Imm = o.Imm;
264 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000265 case MemBarrierOpt:
266 MBOpt = o.MBOpt;
267 break;
Sean Callanan76264762010-04-02 22:27:05 +0000268 case Memory:
269 Mem = o.Mem;
270 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000271 case MSRMask:
272 MMask = o.MMask;
273 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000274 case ProcIFlags:
275 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000276 break;
277 case Shifter:
278 Shift = o.Shift;
279 break;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000280 case ShiftedRegister:
281 ShiftedReg = o.ShiftedReg;
282 break;
Sean Callanan76264762010-04-02 22:27:05 +0000283 }
284 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000285
Sean Callanan76264762010-04-02 22:27:05 +0000286 /// getStartLoc - Get the location of the first token of this operand.
287 SMLoc getStartLoc() const { return StartLoc; }
288 /// getEndLoc - Get the location of the last token of this operand.
289 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000290
Daniel Dunbar8462b302010-08-11 06:36:53 +0000291 ARMCC::CondCodes getCondCode() const {
292 assert(Kind == CondCode && "Invalid access!");
293 return CC.Val;
294 }
295
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000296 unsigned getCoproc() const {
297 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
298 return Cop.Val;
299 }
300
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000301 StringRef getToken() const {
302 assert(Kind == Token && "Invalid access!");
303 return StringRef(Tok.Data, Tok.Length);
304 }
305
306 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000307 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000308 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000309 }
310
Bill Wendling5fa22a12010-11-09 23:28:44 +0000311 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000312 assert((Kind == RegisterList || Kind == DPRRegisterList ||
313 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000314 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000315 }
316
Kevin Enderbycfe07242009-10-13 22:19:02 +0000317 const MCExpr *getImm() const {
318 assert(Kind == Immediate && "Invalid access!");
319 return Imm.Val;
320 }
321
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000322 ARM_MB::MemBOpt getMemBarrierOpt() const {
323 assert(Kind == MemBarrierOpt && "Invalid access!");
324 return MBOpt.Val;
325 }
326
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000327 ARM_PROC::IFlags getProcIFlags() const {
328 assert(Kind == ProcIFlags && "Invalid access!");
329 return IFlags.Val;
330 }
331
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000332 unsigned getMSRMask() const {
333 assert(Kind == MSRMask && "Invalid access!");
334 return MMask.Val;
335 }
336
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000337 /// @name Memory Operand Accessors
338 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000339 ARMII::AddrMode getMemAddrMode() const {
340 return Mem.AddrMode;
341 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000342 unsigned getMemBaseRegNum() const {
343 return Mem.BaseRegNum;
344 }
345 unsigned getMemOffsetRegNum() const {
346 assert(Mem.OffsetIsReg && "Invalid access!");
347 return Mem.Offset.RegNum;
348 }
349 const MCExpr *getMemOffset() const {
350 assert(!Mem.OffsetIsReg && "Invalid access!");
351 return Mem.Offset.Value;
352 }
353 unsigned getMemOffsetRegShifted() const {
354 assert(Mem.OffsetIsReg && "Invalid access!");
355 return Mem.OffsetRegShifted;
356 }
357 const MCExpr *getMemShiftAmount() const {
358 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
359 return Mem.ShiftAmount;
360 }
Owen Anderson00828302011-03-18 22:50:18 +0000361 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000362 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
363 return Mem.ShiftType;
364 }
365 bool getMemPreindexed() const { return Mem.Preindexed; }
366 bool getMemPostindexed() const { return Mem.Postindexed; }
367 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
368 bool getMemNegative() const { return Mem.Negative; }
369 bool getMemWriteback() const { return Mem.Writeback; }
370
371 /// @}
372
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000373 bool isCoprocNum() const { return Kind == CoprocNum; }
374 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000375 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000376 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000377 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000378 bool isImm0_255() const {
379 if (Kind != Immediate)
380 return false;
381 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
382 if (!CE) return false;
383 int64_t Value = CE->getValue();
384 return Value >= 0 && Value < 256;
385 }
Jim Grosbach83ab0702011-07-13 22:01:08 +0000386 bool isImm0_7() const {
387 if (Kind != Immediate)
388 return false;
389 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
390 if (!CE) return false;
391 int64_t Value = CE->getValue();
392 return Value >= 0 && Value < 8;
393 }
394 bool isImm0_15() const {
395 if (Kind != Immediate)
396 return false;
397 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
398 if (!CE) return false;
399 int64_t Value = CE->getValue();
400 return Value >= 0 && Value < 16;
401 }
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000402 bool isImm0_65535() const {
403 if (Kind != Immediate)
404 return false;
405 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
406 if (!CE) return false;
407 int64_t Value = CE->getValue();
408 return Value >= 0 && Value < 65536;
409 }
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000410 bool isARMSOImm() const {
411 if (Kind != Immediate)
412 return false;
413 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
414 if (!CE) return false;
415 int64_t Value = CE->getValue();
416 return ARM_AM::getSOImmVal(Value) != -1;
417 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000418 bool isT2SOImm() const {
419 if (Kind != Immediate)
420 return false;
421 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
422 if (!CE) return false;
423 int64_t Value = CE->getValue();
424 return ARM_AM::getT2SOImmVal(Value) != -1;
425 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000426 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000427 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000428 bool isDPRRegList() const { return Kind == DPRRegisterList; }
429 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000430 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000431 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000432 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000433 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000434 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000435 bool isMemMode2() const {
436 if (getMemAddrMode() != ARMII::AddrMode2)
437 return false;
438
439 if (getMemOffsetIsReg())
440 return true;
441
442 if (getMemNegative() &&
443 !(getMemPostindexed() || getMemPreindexed()))
444 return false;
445
446 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
447 if (!CE) return false;
448 int64_t Value = CE->getValue();
449
450 // The offset must be in the range 0-4095 (imm12).
451 if (Value > 4095 || Value < -4095)
452 return false;
453
454 return true;
455 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000456 bool isMemMode3() const {
457 if (getMemAddrMode() != ARMII::AddrMode3)
458 return false;
459
460 if (getMemOffsetIsReg()) {
461 if (getMemOffsetRegShifted())
462 return false; // No shift with offset reg allowed
463 return true;
464 }
465
466 if (getMemNegative() &&
467 !(getMemPostindexed() || getMemPreindexed()))
468 return false;
469
470 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
471 if (!CE) return false;
472 int64_t Value = CE->getValue();
473
474 // The offset must be in the range 0-255 (imm8).
475 if (Value > 255 || Value < -255)
476 return false;
477
478 return true;
479 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000480 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000481 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
482 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000483 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000484
Daniel Dunbar4b462672011-01-18 05:55:27 +0000485 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000486 if (!CE) return false;
487
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000488 // The offset must be a multiple of 4 in the range 0-1020.
489 int64_t Value = CE->getValue();
490 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
491 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000492 bool isMemMode7() const {
493 if (!isMemory() ||
494 getMemPreindexed() ||
495 getMemPostindexed() ||
496 getMemOffsetIsReg() ||
497 getMemNegative() ||
498 getMemWriteback())
499 return false;
500
501 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
502 if (!CE) return false;
503
504 if (CE->getValue())
505 return false;
506
507 return true;
508 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000509 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000510 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000511 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000512 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000513 }
514 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000515 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000516 return false;
517
Daniel Dunbar4b462672011-01-18 05:55:27 +0000518 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000519 if (!CE) return false;
520
521 // The offset must be a multiple of 4 in the range 0-124.
522 uint64_t Value = CE->getValue();
523 return ((Value & 0x3) == 0 && Value <= 124);
524 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000525 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000526 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000527
528 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000529 // Add as immediates when possible. Null MCExpr = 0.
530 if (Expr == 0)
531 Inst.addOperand(MCOperand::CreateImm(0));
532 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000533 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
534 else
535 Inst.addOperand(MCOperand::CreateExpr(Expr));
536 }
537
Daniel Dunbar8462b302010-08-11 06:36:53 +0000538 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000539 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000540 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000541 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
542 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000543 }
544
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000545 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
546 assert(N == 1 && "Invalid number of operands!");
547 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
548 }
549
550 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
551 assert(N == 1 && "Invalid number of operands!");
552 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
553 }
554
Jim Grosbachd67641b2010-12-06 18:21:12 +0000555 void addCCOutOperands(MCInst &Inst, unsigned N) const {
556 assert(N == 1 && "Invalid number of operands!");
557 Inst.addOperand(MCOperand::CreateReg(getReg()));
558 }
559
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000560 void addRegOperands(MCInst &Inst, unsigned N) const {
561 assert(N == 1 && "Invalid number of operands!");
562 Inst.addOperand(MCOperand::CreateReg(getReg()));
563 }
564
Jim Grosbache8606dc2011-07-13 17:50:29 +0000565 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
566 assert(N == 3 && "Invalid number of operands!");
567 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
568 assert((ShiftedReg.ShiftReg == 0 ||
569 ARM_AM::getSORegOffset(ShiftedReg.ShiftImm) == 0) &&
570 "Invalid shifted register operand!");
571 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
572 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
573 Inst.addOperand(MCOperand::CreateImm(
574 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
575 }
576
Owen Anderson00828302011-03-18 22:50:18 +0000577 void addShifterOperands(MCInst &Inst, unsigned N) const {
578 assert(N == 1 && "Invalid number of operands!");
579 Inst.addOperand(MCOperand::CreateImm(
580 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
581 }
582
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000583 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000584 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000585 const SmallVectorImpl<unsigned> &RegList = getRegList();
586 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000587 I = RegList.begin(), E = RegList.end(); I != E; ++I)
588 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000589 }
590
Bill Wendling0f630752010-11-17 04:32:08 +0000591 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
592 addRegListOperands(Inst, N);
593 }
594
595 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
596 addRegListOperands(Inst, N);
597 }
598
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000599 void addImmOperands(MCInst &Inst, unsigned N) const {
600 assert(N == 1 && "Invalid number of operands!");
601 addExpr(Inst, getImm());
602 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000603
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000604 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
605 assert(N == 1 && "Invalid number of operands!");
606 addExpr(Inst, getImm());
607 }
608
Jim Grosbach83ab0702011-07-13 22:01:08 +0000609 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
610 assert(N == 1 && "Invalid number of operands!");
611 addExpr(Inst, getImm());
612 }
613
614 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
615 assert(N == 1 && "Invalid number of operands!");
616 addExpr(Inst, getImm());
617 }
618
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000619 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
620 assert(N == 1 && "Invalid number of operands!");
621 addExpr(Inst, getImm());
622 }
623
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000624 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
625 assert(N == 1 && "Invalid number of operands!");
626 addExpr(Inst, getImm());
627 }
628
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000629 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
630 assert(N == 1 && "Invalid number of operands!");
631 addExpr(Inst, getImm());
632 }
633
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000634 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
635 assert(N == 1 && "Invalid number of operands!");
636 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
637 }
638
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000639 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
640 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
641 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
642
643 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000644 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000645 assert((CE || CE->getValue() == 0) &&
646 "No offset operand support in mode 7");
647 }
648
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000649 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
650 assert(isMemMode2() && "Invalid mode or number of operands!");
651 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
652 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
653
654 if (getMemOffsetIsReg()) {
655 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
656
657 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
658 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
659 int64_t ShiftAmount = 0;
660
661 if (getMemOffsetRegShifted()) {
662 ShOpc = getMemShiftType();
663 const MCConstantExpr *CE =
664 dyn_cast<MCConstantExpr>(getMemShiftAmount());
665 ShiftAmount = CE->getValue();
666 }
667
668 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
669 ShOpc, IdxMode)));
670 return;
671 }
672
673 // Create a operand placeholder to always yield the same number of operands.
674 Inst.addOperand(MCOperand::CreateReg(0));
675
676 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
677 // the difference?
678 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
679 assert(CE && "Non-constant mode 2 offset operand!");
680 int64_t Offset = CE->getValue();
681
682 if (Offset >= 0)
683 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
684 Offset, ARM_AM::no_shift, IdxMode)));
685 else
686 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
687 -Offset, ARM_AM::no_shift, IdxMode)));
688 }
689
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000690 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
691 assert(isMemMode3() && "Invalid mode or number of operands!");
692 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
693 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
694
695 if (getMemOffsetIsReg()) {
696 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
697
698 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
699 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
700 IdxMode)));
701 return;
702 }
703
704 // Create a operand placeholder to always yield the same number of operands.
705 Inst.addOperand(MCOperand::CreateReg(0));
706
707 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
708 // the difference?
709 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
710 assert(CE && "Non-constant mode 3 offset operand!");
711 int64_t Offset = CE->getValue();
712
713 if (Offset >= 0)
714 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
715 Offset, IdxMode)));
716 else
717 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
718 -Offset, IdxMode)));
719 }
720
Chris Lattner14b93852010-10-29 00:27:31 +0000721 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
722 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000723
Daniel Dunbar4b462672011-01-18 05:55:27 +0000724 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
725 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000726
Jim Grosbach80eb2332010-10-29 17:41:25 +0000727 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
728 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000729 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000730 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000731
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000732 // The MCInst offset operand doesn't include the low two bits (like
733 // the instruction encoding).
734 int64_t Offset = CE->getValue() / 4;
735 if (Offset >= 0)
736 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
737 Offset)));
738 else
739 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
740 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000741 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000742
Bill Wendlingf4caf692010-12-14 03:36:38 +0000743 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
744 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000745 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
746 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000747 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000748
Bill Wendlingf4caf692010-12-14 03:36:38 +0000749 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
750 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000751 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
752 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000753 assert(CE && "Non-constant mode offset operand!");
754 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000755 }
756
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000757 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
758 assert(N == 1 && "Invalid number of operands!");
759 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
760 }
761
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000762 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
763 assert(N == 1 && "Invalid number of operands!");
764 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
765 }
766
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000767 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000768
Chris Lattner3a697562010-10-28 17:20:03 +0000769 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
770 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000771 Op->CC.Val = CC;
772 Op->StartLoc = S;
773 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000774 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000775 }
776
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000777 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
778 ARMOperand *Op = new ARMOperand(CoprocNum);
779 Op->Cop.Val = CopVal;
780 Op->StartLoc = S;
781 Op->EndLoc = S;
782 return Op;
783 }
784
785 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
786 ARMOperand *Op = new ARMOperand(CoprocReg);
787 Op->Cop.Val = CopVal;
788 Op->StartLoc = S;
789 Op->EndLoc = S;
790 return Op;
791 }
792
Jim Grosbachd67641b2010-12-06 18:21:12 +0000793 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
794 ARMOperand *Op = new ARMOperand(CCOut);
795 Op->Reg.RegNum = RegNum;
796 Op->StartLoc = S;
797 Op->EndLoc = S;
798 return Op;
799 }
800
Chris Lattner3a697562010-10-28 17:20:03 +0000801 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
802 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000803 Op->Tok.Data = Str.data();
804 Op->Tok.Length = Str.size();
805 Op->StartLoc = S;
806 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000807 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000808 }
809
Bill Wendling50d0f582010-11-18 23:43:05 +0000810 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000811 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000812 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000813 Op->StartLoc = S;
814 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000815 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000816 }
817
Jim Grosbache8606dc2011-07-13 17:50:29 +0000818 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
819 unsigned SrcReg,
820 unsigned ShiftReg,
821 unsigned ShiftImm,
822 SMLoc S, SMLoc E) {
823 ARMOperand *Op = new ARMOperand(ShiftedRegister);
824 Op->ShiftedReg.ShiftTy = ShTy;
825 Op->ShiftedReg.SrcReg = SrcReg;
826 Op->ShiftedReg.ShiftReg = ShiftReg;
827 Op->ShiftedReg.ShiftImm = ShiftImm;
828 Op->StartLoc = S;
829 Op->EndLoc = E;
830 return Op;
831 }
832
Owen Anderson00828302011-03-18 22:50:18 +0000833 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
834 SMLoc S, SMLoc E) {
835 ARMOperand *Op = new ARMOperand(Shifter);
836 Op->Shift.ShiftTy = ShTy;
837 Op->StartLoc = S;
838 Op->EndLoc = E;
839 return Op;
840 }
841
Bill Wendling7729e062010-11-09 22:44:22 +0000842 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000843 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000844 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000845 KindTy Kind = RegisterList;
846
847 if (ARM::DPRRegClass.contains(Regs.front().first))
848 Kind = DPRRegisterList;
849 else if (ARM::SPRRegClass.contains(Regs.front().first))
850 Kind = SPRRegisterList;
851
852 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000853 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000854 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000855 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000856 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000857 Op->StartLoc = StartLoc;
858 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000859 return Op;
860 }
861
Chris Lattner3a697562010-10-28 17:20:03 +0000862 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
863 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000864 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000865 Op->StartLoc = S;
866 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000867 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000868 }
869
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000870 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
871 bool OffsetIsReg, const MCExpr *Offset,
872 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000873 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000874 const MCExpr *ShiftAmount, bool Preindexed,
875 bool Postindexed, bool Negative, bool Writeback,
876 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000877 assert((OffsetRegNum == -1 || OffsetIsReg) &&
878 "OffsetRegNum must imply OffsetIsReg!");
879 assert((!OffsetRegShifted || OffsetIsReg) &&
880 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000881 assert((Offset || OffsetIsReg) &&
882 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000883 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
884 "Cannot have shift amount without shifted register offset!");
885 assert((!Offset || !OffsetIsReg) &&
886 "Cannot have expression offset and register offset!");
887
Chris Lattner3a697562010-10-28 17:20:03 +0000888 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000889 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000890 Op->Mem.BaseRegNum = BaseRegNum;
891 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000892 if (OffsetIsReg)
893 Op->Mem.Offset.RegNum = OffsetRegNum;
894 else
895 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000896 Op->Mem.OffsetRegShifted = OffsetRegShifted;
897 Op->Mem.ShiftType = ShiftType;
898 Op->Mem.ShiftAmount = ShiftAmount;
899 Op->Mem.Preindexed = Preindexed;
900 Op->Mem.Postindexed = Postindexed;
901 Op->Mem.Negative = Negative;
902 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000903
Sean Callanan76264762010-04-02 22:27:05 +0000904 Op->StartLoc = S;
905 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000906 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000907 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000908
909 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
910 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
911 Op->MBOpt.Val = Opt;
912 Op->StartLoc = S;
913 Op->EndLoc = S;
914 return Op;
915 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000916
917 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
918 ARMOperand *Op = new ARMOperand(ProcIFlags);
919 Op->IFlags.Val = IFlags;
920 Op->StartLoc = S;
921 Op->EndLoc = S;
922 return Op;
923 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000924
925 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
926 ARMOperand *Op = new ARMOperand(MSRMask);
927 Op->MMask.Val = MMask;
928 Op->StartLoc = S;
929 Op->EndLoc = S;
930 return Op;
931 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000932};
933
934} // end anonymous namespace.
935
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000936void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000937 switch (Kind) {
938 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000939 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000940 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000941 case CCOut:
942 OS << "<ccout " << getReg() << ">";
943 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000944 case CoprocNum:
945 OS << "<coprocessor number: " << getCoproc() << ">";
946 break;
947 case CoprocReg:
948 OS << "<coprocessor register: " << getCoproc() << ">";
949 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000950 case MSRMask:
951 OS << "<mask: " << getMSRMask() << ">";
952 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000953 case Immediate:
954 getImm()->print(OS);
955 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000956 case MemBarrierOpt:
957 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
958 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000959 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000960 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000961 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
962 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000963 if (getMemOffsetIsReg()) {
964 OS << " offset:<register " << getMemOffsetRegNum();
965 if (getMemOffsetRegShifted()) {
966 OS << " offset-shift-type:" << getMemShiftType();
967 OS << " offset-shift-amount:" << *getMemShiftAmount();
968 }
969 } else {
970 OS << " offset:" << *getMemOffset();
971 }
972 if (getMemOffsetIsReg())
973 OS << " (offset-is-reg)";
974 if (getMemPreindexed())
975 OS << " (pre-indexed)";
976 if (getMemPostindexed())
977 OS << " (post-indexed)";
978 if (getMemNegative())
979 OS << " (negative)";
980 if (getMemWriteback())
981 OS << " (writeback)";
982 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000983 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000984 case ProcIFlags: {
985 OS << "<ARM_PROC::";
986 unsigned IFlags = getProcIFlags();
987 for (int i=2; i >= 0; --i)
988 if (IFlags & (1 << i))
989 OS << ARM_PROC::IFlagsToString(1 << i);
990 OS << ">";
991 break;
992 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000993 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000994 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000995 break;
Owen Anderson00828302011-03-18 22:50:18 +0000996 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +0000997 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
998 break;
999 case ShiftedRegister:
1000 OS << "<so_reg"
1001 << ShiftedReg.SrcReg
1002 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
1003 << ", " << ShiftedReg.ShiftReg << ", "
1004 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
1005 << ">";
Owen Anderson00828302011-03-18 22:50:18 +00001006 break;
Bill Wendling0f630752010-11-17 04:32:08 +00001007 case RegisterList:
1008 case DPRRegisterList:
1009 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00001010 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001011
Bill Wendling5fa22a12010-11-09 23:28:44 +00001012 const SmallVectorImpl<unsigned> &RegList = getRegList();
1013 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001014 I = RegList.begin(), E = RegList.end(); I != E; ) {
1015 OS << *I;
1016 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001017 }
1018
1019 OS << ">";
1020 break;
1021 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001022 case Token:
1023 OS << "'" << getToken() << "'";
1024 break;
1025 }
1026}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001027
1028/// @name Auto-generated Match Functions
1029/// {
1030
1031static unsigned MatchRegisterName(StringRef Name);
1032
1033/// }
1034
Bob Wilson69df7232011-02-03 21:46:10 +00001035bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1036 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +00001037 RegNo = TryParseRegister();
1038
1039 return (RegNo == (unsigned)-1);
1040}
1041
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001042/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00001043/// and if it is a register name the token is eaten and the register number is
1044/// returned. Otherwise return -1.
1045///
1046int ARMAsmParser::TryParseRegister() {
1047 const AsmToken &Tok = Parser.getTok();
1048 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001049
Chris Lattnere5658fa2010-10-30 04:09:10 +00001050 // FIXME: Validate register for the current architecture; we have to do
1051 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001052 std::string upperCase = Tok.getString().str();
1053 std::string lowerCase = LowercaseString(upperCase);
1054 unsigned RegNum = MatchRegisterName(lowerCase);
1055 if (!RegNum) {
1056 RegNum = StringSwitch<unsigned>(lowerCase)
1057 .Case("r13", ARM::SP)
1058 .Case("r14", ARM::LR)
1059 .Case("r15", ARM::PC)
1060 .Case("ip", ARM::R12)
1061 .Default(0);
1062 }
1063 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001064
Chris Lattnere5658fa2010-10-30 04:09:10 +00001065 Parser.Lex(); // Eat identifier token.
1066 return RegNum;
1067}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001068
Jim Grosbach19906722011-07-13 18:49:30 +00001069// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1070// If a recoverable error occurs, return 1. If an irrecoverable error
1071// occurs, return -1. An irrecoverable error is one where tokens have been
1072// consumed in the process of trying to parse the shifter (i.e., when it is
1073// indeed a shifter operand, but malformed).
1074int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001075 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1076 SMLoc S = Parser.getTok().getLoc();
1077 const AsmToken &Tok = Parser.getTok();
1078 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1079
1080 std::string upperCase = Tok.getString().str();
1081 std::string lowerCase = LowercaseString(upperCase);
1082 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1083 .Case("lsl", ARM_AM::lsl)
1084 .Case("lsr", ARM_AM::lsr)
1085 .Case("asr", ARM_AM::asr)
1086 .Case("ror", ARM_AM::ror)
1087 .Case("rrx", ARM_AM::rrx)
1088 .Default(ARM_AM::no_shift);
1089
1090 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001091 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001092
Jim Grosbache8606dc2011-07-13 17:50:29 +00001093 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001094
Jim Grosbache8606dc2011-07-13 17:50:29 +00001095 // The source register for the shift has already been added to the
1096 // operand list, so we need to pop it off and combine it into the shifted
1097 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00001098 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00001099 if (!PrevOp->isReg())
1100 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1101 int SrcReg = PrevOp->getReg();
1102 int64_t Imm = 0;
1103 int ShiftReg = 0;
1104 if (ShiftTy == ARM_AM::rrx) {
1105 // RRX Doesn't have an explicit shift amount. The encoder expects
1106 // the shift register to be the same as the source register. Seems odd,
1107 // but OK.
1108 ShiftReg = SrcReg;
1109 } else {
1110 // Figure out if this is shifted by a constant or a register (for non-RRX).
1111 if (Parser.getTok().is(AsmToken::Hash)) {
1112 Parser.Lex(); // Eat hash.
1113 SMLoc ImmLoc = Parser.getTok().getLoc();
1114 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001115 if (getParser().ParseExpression(ShiftExpr)) {
1116 Error(ImmLoc, "invalid immediate shift value");
1117 return -1;
1118 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001119 // The expression must be evaluatable as an immediate.
1120 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001121 if (!CE) {
1122 Error(ImmLoc, "invalid immediate shift value");
1123 return -1;
1124 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001125 // Range check the immediate.
1126 // lsl, ror: 0 <= imm <= 31
1127 // lsr, asr: 0 <= imm <= 32
1128 Imm = CE->getValue();
1129 if (Imm < 0 ||
1130 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1131 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001132 Error(ImmLoc, "immediate shift value out of range");
1133 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001134 }
1135 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1136 ShiftReg = TryParseRegister();
1137 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001138 if (ShiftReg == -1) {
1139 Error (L, "expected immediate or register in shift operand");
1140 return -1;
1141 }
1142 } else {
1143 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001144 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001145 return -1;
1146 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001147 }
1148
Jim Grosbache8606dc2011-07-13 17:50:29 +00001149 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1150 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001151 S, Parser.getTok().getLoc()));
1152
Jim Grosbach19906722011-07-13 18:49:30 +00001153 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001154}
1155
1156
Bill Wendling50d0f582010-11-18 23:43:05 +00001157/// Try to parse a register name. The token must be an Identifier when called.
1158/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1159/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001160///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001161/// TODO this is likely to change to allow different register types and or to
1162/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001163bool ARMAsmParser::
1164TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001165 SMLoc S = Parser.getTok().getLoc();
1166 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001167 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001168 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001169
Bill Wendling50d0f582010-11-18 23:43:05 +00001170 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001171
Chris Lattnere5658fa2010-10-30 04:09:10 +00001172 const AsmToken &ExclaimTok = Parser.getTok();
1173 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001174 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1175 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001176 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001177 }
1178
Bill Wendling50d0f582010-11-18 23:43:05 +00001179 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001180}
1181
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001182/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1183/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1184/// "c5", ...
1185static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001186 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1187 // but efficient.
1188 switch (Name.size()) {
1189 default: break;
1190 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001191 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001192 return -1;
1193 switch (Name[1]) {
1194 default: return -1;
1195 case '0': return 0;
1196 case '1': return 1;
1197 case '2': return 2;
1198 case '3': return 3;
1199 case '4': return 4;
1200 case '5': return 5;
1201 case '6': return 6;
1202 case '7': return 7;
1203 case '8': return 8;
1204 case '9': return 9;
1205 }
1206 break;
1207 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001208 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001209 return -1;
1210 switch (Name[2]) {
1211 default: return -1;
1212 case '0': return 10;
1213 case '1': return 11;
1214 case '2': return 12;
1215 case '3': return 13;
1216 case '4': return 14;
1217 case '5': return 15;
1218 }
1219 break;
1220 }
1221
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001222 return -1;
1223}
1224
Jim Grosbachf922c472011-02-12 01:34:40 +00001225/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001226/// token must be an Identifier when called, and if it is a coprocessor
1227/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001228ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1229tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001230 SMLoc S = Parser.getTok().getLoc();
1231 const AsmToken &Tok = Parser.getTok();
1232 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1233
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001234 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001235 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001236 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001237
1238 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001239 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001240 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001241}
1242
Jim Grosbachf922c472011-02-12 01:34:40 +00001243/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001244/// token must be an Identifier when called, and if it is a coprocessor
1245/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001246ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1247tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001248 SMLoc S = Parser.getTok().getLoc();
1249 const AsmToken &Tok = Parser.getTok();
1250 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1251
1252 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1253 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001254 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001255
1256 Parser.Lex(); // Eat identifier token.
1257 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001258 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001259}
1260
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001261/// Parse a register list, return it if successful else return null. The first
1262/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001263bool ARMAsmParser::
1264ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001265 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001266 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001267 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001268
Bill Wendling7729e062010-11-09 22:44:22 +00001269 // Read the rest of the registers in the list.
1270 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001271 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001272
Bill Wendling7729e062010-11-09 22:44:22 +00001273 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001274 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001275 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001276
Sean Callanan18b83232010-01-19 21:44:56 +00001277 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001278 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001279 if (RegTok.isNot(AsmToken::Identifier)) {
1280 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001281 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001282 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001283
Bill Wendling1d6a2652010-11-06 10:40:24 +00001284 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001285 if (RegNum == -1) {
1286 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001287 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001288 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001289
Bill Wendlinge7176102010-11-06 22:36:58 +00001290 if (IsRange) {
1291 int Reg = PrevRegNum;
1292 do {
1293 ++Reg;
1294 Registers.push_back(std::make_pair(Reg, RegLoc));
1295 } while (Reg != RegNum);
1296 } else {
1297 Registers.push_back(std::make_pair(RegNum, RegLoc));
1298 }
1299
1300 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001301 } while (Parser.getTok().is(AsmToken::Comma) ||
1302 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001303
1304 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001305 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001306 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1307 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001308 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001309 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001310
Bill Wendlinge7176102010-11-06 22:36:58 +00001311 SMLoc E = RCurlyTok.getLoc();
1312 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001313
Bill Wendlinge7176102010-11-06 22:36:58 +00001314 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001315 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001316 RI = Registers.begin(), RE = Registers.end();
1317
Bill Wendling7caebff2011-01-12 21:20:59 +00001318 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001319 bool EmittedWarning = false;
1320
Bill Wendling7caebff2011-01-12 21:20:59 +00001321 DenseMap<unsigned, bool> RegMap;
1322 RegMap[HighRegNum] = true;
1323
Bill Wendlinge7176102010-11-06 22:36:58 +00001324 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001325 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001326 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001327
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001328 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001329 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001330 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001331 }
1332
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001333 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001334 Warning(RegInfo.second,
1335 "register not in ascending order in register list");
1336
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001337 RegMap[Reg] = true;
1338 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001339 }
1340
Bill Wendling50d0f582010-11-18 23:43:05 +00001341 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1342 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001343}
1344
Jim Grosbachf922c472011-02-12 01:34:40 +00001345/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1346ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1347tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001348 SMLoc S = Parser.getTok().getLoc();
1349 const AsmToken &Tok = Parser.getTok();
1350 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1351 StringRef OptStr = Tok.getString();
1352
1353 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1354 .Case("sy", ARM_MB::SY)
1355 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001356 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001357 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001358 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001359 .Case("ishst", ARM_MB::ISHST)
1360 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001361 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001362 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001363 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001364 .Case("osh", ARM_MB::OSH)
1365 .Case("oshst", ARM_MB::OSHST)
1366 .Default(~0U);
1367
1368 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001369 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001370
1371 Parser.Lex(); // Eat identifier token.
1372 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001373 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001374}
1375
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001376/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001377ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1378tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1379 SMLoc S = Parser.getTok().getLoc();
1380 const AsmToken &Tok = Parser.getTok();
1381 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1382 StringRef IFlagsStr = Tok.getString();
1383
1384 unsigned IFlags = 0;
1385 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1386 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1387 .Case("a", ARM_PROC::A)
1388 .Case("i", ARM_PROC::I)
1389 .Case("f", ARM_PROC::F)
1390 .Default(~0U);
1391
1392 // If some specific iflag is already set, it means that some letter is
1393 // present more than once, this is not acceptable.
1394 if (Flag == ~0U || (IFlags & Flag))
1395 return MatchOperand_NoMatch;
1396
1397 IFlags |= Flag;
1398 }
1399
1400 Parser.Lex(); // Eat identifier token.
1401 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1402 return MatchOperand_Success;
1403}
1404
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001405/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1406ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1407tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1408 SMLoc S = Parser.getTok().getLoc();
1409 const AsmToken &Tok = Parser.getTok();
1410 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1411 StringRef Mask = Tok.getString();
1412
1413 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1414 size_t Start = 0, Next = Mask.find('_');
1415 StringRef Flags = "";
1416 StringRef SpecReg = Mask.slice(Start, Next);
1417 if (Next != StringRef::npos)
1418 Flags = Mask.slice(Next+1, Mask.size());
1419
1420 // FlagsVal contains the complete mask:
1421 // 3-0: Mask
1422 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1423 unsigned FlagsVal = 0;
1424
1425 if (SpecReg == "apsr") {
1426 FlagsVal = StringSwitch<unsigned>(Flags)
1427 .Case("nzcvq", 0x8) // same as CPSR_c
1428 .Case("g", 0x4) // same as CPSR_s
1429 .Case("nzcvqg", 0xc) // same as CPSR_fs
1430 .Default(~0U);
1431
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001432 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001433 if (!Flags.empty())
1434 return MatchOperand_NoMatch;
1435 else
1436 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001437 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001438 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001439 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1440 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001441 for (int i = 0, e = Flags.size(); i != e; ++i) {
1442 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1443 .Case("c", 1)
1444 .Case("x", 2)
1445 .Case("s", 4)
1446 .Case("f", 8)
1447 .Default(~0U);
1448
1449 // If some specific flag is already set, it means that some letter is
1450 // present more than once, this is not acceptable.
1451 if (FlagsVal == ~0U || (FlagsVal & Flag))
1452 return MatchOperand_NoMatch;
1453 FlagsVal |= Flag;
1454 }
1455 } else // No match for special register.
1456 return MatchOperand_NoMatch;
1457
1458 // Special register without flags are equivalent to "fc" flags.
1459 if (!FlagsVal)
1460 FlagsVal = 0x9;
1461
1462 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1463 if (SpecReg == "spsr")
1464 FlagsVal |= 16;
1465
1466 Parser.Lex(); // Eat identifier token.
1467 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1468 return MatchOperand_Success;
1469}
1470
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001471/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1472ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1473tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001474 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001475
1476 if (ParseMemory(Operands, ARMII::AddrMode2))
1477 return MatchOperand_NoMatch;
1478
1479 return MatchOperand_Success;
1480}
1481
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001482/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1483ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1484tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1485 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1486
1487 if (ParseMemory(Operands, ARMII::AddrMode3))
1488 return MatchOperand_NoMatch;
1489
1490 return MatchOperand_Success;
1491}
1492
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001493/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1494/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1495/// when they refer multiple MIOperands inside a single one.
1496bool ARMAsmParser::
1497CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1498 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1499 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1500
1501 // Create a writeback register dummy placeholder.
1502 Inst.addOperand(MCOperand::CreateImm(0));
1503
1504 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1505 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1506 return true;
1507}
1508
1509/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1510/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1511/// when they refer multiple MIOperands inside a single one.
1512bool ARMAsmParser::
1513CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1514 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1515 // Create a writeback register dummy placeholder.
1516 Inst.addOperand(MCOperand::CreateImm(0));
1517 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1518 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1519 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1520 return true;
1521}
1522
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001523/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1524/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1525/// when they refer multiple MIOperands inside a single one.
1526bool ARMAsmParser::
1527CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1528 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1529 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1530
1531 // Create a writeback register dummy placeholder.
1532 Inst.addOperand(MCOperand::CreateImm(0));
1533
1534 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1535 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1536 return true;
1537}
1538
1539/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1540/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1541/// when they refer multiple MIOperands inside a single one.
1542bool ARMAsmParser::
1543CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1544 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1545 // Create a writeback register dummy placeholder.
1546 Inst.addOperand(MCOperand::CreateImm(0));
1547 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1548 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1549 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1550 return true;
1551}
1552
Bill Wendlinge7176102010-11-06 22:36:58 +00001553/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001554/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001555///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001556/// TODO Only preindexing and postindexing addressing are started, unindexed
1557/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001558bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001559ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1560 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001561 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001562 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001563 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001564 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001565 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001566
Sean Callanan18b83232010-01-19 21:44:56 +00001567 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001568 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1569 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001570 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001571 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001572 int BaseRegNum = TryParseRegister();
1573 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001574 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001575 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001576 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001577
Daniel Dunbar05710932011-01-18 05:34:17 +00001578 // The next token must either be a comma or a closing bracket.
1579 const AsmToken &Tok = Parser.getTok();
1580 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1581 return true;
1582
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001583 bool Preindexed = false;
1584 bool Postindexed = false;
1585 bool OffsetIsReg = false;
1586 bool Negative = false;
1587 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001588 ARMOperand *WBOp = 0;
1589 int OffsetRegNum = -1;
1590 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001591 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001592 const MCExpr *ShiftAmount = 0;
1593 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001594
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001595 // First look for preindexed address forms, that is after the "[Rn" we now
1596 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001597 if (Tok.is(AsmToken::Comma)) {
1598 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001599 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001600
Chris Lattner550276e2010-10-28 20:52:15 +00001601 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1602 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001603 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001604 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001605 if (RBracTok.isNot(AsmToken::RBrac)) {
1606 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001607 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001608 }
Sean Callanan76264762010-04-02 22:27:05 +00001609 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001610 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001611
Sean Callanan18b83232010-01-19 21:44:56 +00001612 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001613 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001614 // None of addrmode3 instruction uses "!"
1615 if (AddrMode == ARMII::AddrMode3)
1616 return true;
1617
Bill Wendling50d0f582010-11-18 23:43:05 +00001618 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1619 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001620 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001621 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001622 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1623 if (AddrMode == ARMII::AddrMode2)
1624 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001625 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001626 } else {
1627 // The "[Rn" we have so far was not followed by a comma.
1628
Jim Grosbach80eb2332010-10-29 17:41:25 +00001629 // If there's anything other than the right brace, this is a post indexing
1630 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001631 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001632 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001633
Sean Callanan18b83232010-01-19 21:44:56 +00001634 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001635
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001636 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001637 Postindexed = true;
1638 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001639
Chris Lattner550276e2010-10-28 20:52:15 +00001640 if (NextTok.isNot(AsmToken::Comma)) {
1641 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001642 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001643 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001644
Sean Callananb9a25b72010-01-19 20:27:46 +00001645 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001646
Chris Lattner550276e2010-10-28 20:52:15 +00001647 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001648 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001649 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001650 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001651 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001652 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001653
1654 // Force Offset to exist if used.
1655 if (!OffsetIsReg) {
1656 if (!Offset)
1657 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001658 } else {
1659 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1660 Error(E, "shift amount not supported");
1661 return true;
1662 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001663 }
1664
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001665 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1666 Offset, OffsetRegNum, OffsetRegShifted,
1667 ShiftType, ShiftAmount, Preindexed,
1668 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001669 if (WBOp)
1670 Operands.push_back(WBOp);
1671
1672 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001673}
1674
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001675/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1676/// we will parse the following (were +/- means that a plus or minus is
1677/// optional):
1678/// +/-Rm
1679/// +/-Rm, shift
1680/// #offset
1681/// we return false on success or an error otherwise.
1682bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001683 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001684 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001685 const MCExpr *&ShiftAmount,
1686 const MCExpr *&Offset,
1687 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001688 int &OffsetRegNum,
1689 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001690 Negative = false;
1691 OffsetRegShifted = false;
1692 OffsetIsReg = false;
1693 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001694 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001695 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001696 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001697 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001698 else if (NextTok.is(AsmToken::Minus)) {
1699 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001700 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001701 }
1702 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001703 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001704 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001705 SMLoc CurLoc = OffsetRegTok.getLoc();
1706 OffsetRegNum = TryParseRegister();
1707 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001708 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001709 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001710 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001711 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001712
Bill Wendling12f40e92010-11-06 10:51:53 +00001713 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001714 if (OffsetRegNum != -1) {
1715 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001716 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001717 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001718 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001719
Sean Callanan18b83232010-01-19 21:44:56 +00001720 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001721 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001722 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001723 OffsetRegShifted = true;
1724 }
1725 }
1726 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1727 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001728 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001729 if (HashTok.isNot(AsmToken::Hash))
1730 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001731
Sean Callananb9a25b72010-01-19 20:27:46 +00001732 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001733
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001734 if (getParser().ParseExpression(Offset))
1735 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001736 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001737 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001738 return false;
1739}
1740
1741/// ParseShift as one of these two:
1742/// ( lsl | lsr | asr | ror ) , # shift_amount
1743/// rrx
1744/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001745bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1746 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001747 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001748 if (Tok.isNot(AsmToken::Identifier))
1749 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001750 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001751 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001752 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001753 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001754 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001755 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001756 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001757 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001758 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001759 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001760 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001761 else
1762 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001763 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001764
1765 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001766 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001767 return false;
1768
1769 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001770 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001771 if (HashTok.isNot(AsmToken::Hash))
1772 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001773 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001774
1775 if (getParser().ParseExpression(ShiftAmount))
1776 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001777
1778 return false;
1779}
1780
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001781/// Parse a arm instruction operand. For now this parses the operand regardless
1782/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001783bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001784 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001785 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001786
1787 // Check if the current operand has a custom associated parser, if so, try to
1788 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001789 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1790 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001791 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001792 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1793 // there was a match, but an error occurred, in which case, just return that
1794 // the operand parsing failed.
1795 if (ResTy == MatchOperand_ParseFail)
1796 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001797
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001798 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001799 default:
1800 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001801 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001802 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001803 if (!TryParseRegisterWithWriteBack(Operands))
1804 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001805 int Res = TryParseShiftRegister(Operands);
1806 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001807 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001808 else if (Res == -1) // irrecoverable error
1809 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001810
1811 // Fall though for the Identifier case that is not a register or a
1812 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00001813 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00001814 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1815 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001816 // This was not a register so parse other operands that start with an
1817 // identifier (like labels) as expressions and create them as immediates.
1818 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001819 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001820 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001821 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001822 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001823 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1824 return false;
1825 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001826 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001827 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001828 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001829 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001830 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001831 // #42 -> immediate.
1832 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001833 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001834 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001835 const MCExpr *ImmVal;
1836 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001837 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001838 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001839 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1840 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001841 case AsmToken::Colon: {
1842 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001843 // FIXME: Check it's an expression prefix,
1844 // e.g. (FOO - :lower16:BAR) isn't legal.
1845 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001846 if (ParsePrefix(RefKind))
1847 return true;
1848
Evan Cheng75972122011-01-13 07:58:56 +00001849 const MCExpr *SubExprVal;
1850 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001851 return true;
1852
Evan Cheng75972122011-01-13 07:58:56 +00001853 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1854 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001855 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001856 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001857 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001858 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001859 }
1860}
1861
Evan Cheng75972122011-01-13 07:58:56 +00001862// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1863// :lower16: and :upper16:.
1864bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1865 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001866
1867 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001868 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001869 Parser.Lex(); // Eat ':'
1870
1871 if (getLexer().isNot(AsmToken::Identifier)) {
1872 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1873 return true;
1874 }
1875
1876 StringRef IDVal = Parser.getTok().getIdentifier();
1877 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001878 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001879 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001880 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001881 } else {
1882 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1883 return true;
1884 }
1885 Parser.Lex();
1886
1887 if (getLexer().isNot(AsmToken::Colon)) {
1888 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1889 return true;
1890 }
1891 Parser.Lex(); // Eat the last ':'
1892 return false;
1893}
1894
1895const MCExpr *
1896ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1897 MCSymbolRefExpr::VariantKind Variant) {
1898 // Recurse over the given expression, rebuilding it to apply the given variant
1899 // to the leftmost symbol.
1900 if (Variant == MCSymbolRefExpr::VK_None)
1901 return E;
1902
1903 switch (E->getKind()) {
1904 case MCExpr::Target:
1905 llvm_unreachable("Can't handle target expr yet");
1906 case MCExpr::Constant:
1907 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1908
1909 case MCExpr::SymbolRef: {
1910 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1911
1912 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1913 return 0;
1914
1915 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1916 }
1917
1918 case MCExpr::Unary:
1919 llvm_unreachable("Can't handle unary expressions yet");
1920
1921 case MCExpr::Binary: {
1922 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1923 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1924 const MCExpr *RHS = BE->getRHS();
1925 if (!LHS)
1926 return 0;
1927
1928 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1929 }
1930 }
1931
1932 assert(0 && "Invalid expression kind!");
1933 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001934}
1935
Daniel Dunbar352e1482011-01-11 15:59:50 +00001936/// \brief Given a mnemonic, split out possible predication code and carry
1937/// setting letters to form a canonical mnemonic and flags.
1938//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001939// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001940static StringRef SplitMnemonic(StringRef Mnemonic,
1941 unsigned &PredicationCode,
1942 bool &CarrySetting,
1943 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001944 PredicationCode = ARMCC::AL;
1945 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001946 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001947
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001948 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001949 //
1950 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001951 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1952 Mnemonic == "movs" ||
1953 Mnemonic == "svc" ||
1954 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1955 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1956 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1957 Mnemonic == "vclt" ||
1958 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1959 Mnemonic == "vcle" ||
1960 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1961 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbach5a187002011-07-19 18:32:48 +00001962 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001963 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001964
Jim Grosbach3f00e312011-07-11 17:09:57 +00001965 // First, split out any predication code. Ignore mnemonics we know aren't
1966 // predicated but do have a carry-set and so weren't caught above.
Jim Grosbach5a187002011-07-19 18:32:48 +00001967 if (Mnemonic != "adcs" && Mnemonic != "bics") {
Jim Grosbach3f00e312011-07-11 17:09:57 +00001968 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1969 .Case("eq", ARMCC::EQ)
1970 .Case("ne", ARMCC::NE)
1971 .Case("hs", ARMCC::HS)
1972 .Case("cs", ARMCC::HS)
1973 .Case("lo", ARMCC::LO)
1974 .Case("cc", ARMCC::LO)
1975 .Case("mi", ARMCC::MI)
1976 .Case("pl", ARMCC::PL)
1977 .Case("vs", ARMCC::VS)
1978 .Case("vc", ARMCC::VC)
1979 .Case("hi", ARMCC::HI)
1980 .Case("ls", ARMCC::LS)
1981 .Case("ge", ARMCC::GE)
1982 .Case("lt", ARMCC::LT)
1983 .Case("gt", ARMCC::GT)
1984 .Case("le", ARMCC::LE)
1985 .Case("al", ARMCC::AL)
1986 .Default(~0U);
1987 if (CC != ~0U) {
1988 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
1989 PredicationCode = CC;
1990 }
Bill Wendling52925b62010-10-29 23:50:21 +00001991 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001992
Daniel Dunbar352e1482011-01-11 15:59:50 +00001993 // Next, determine if we have a carry setting bit. We explicitly ignore all
1994 // the instructions we know end in 's'.
1995 if (Mnemonic.endswith("s") &&
1996 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1997 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1998 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1999 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
2000 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
2001 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
2002 CarrySetting = true;
2003 }
2004
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002005 // The "cps" instruction can have a interrupt mode operand which is glued into
2006 // the mnemonic. Check if this is the case, split it and parse the imod op
2007 if (Mnemonic.startswith("cps")) {
2008 // Split out any imod code.
2009 unsigned IMod =
2010 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
2011 .Case("ie", ARM_PROC::IE)
2012 .Case("id", ARM_PROC::ID)
2013 .Default(~0U);
2014 if (IMod != ~0U) {
2015 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2016 ProcessorIMod = IMod;
2017 }
2018 }
2019
Daniel Dunbar352e1482011-01-11 15:59:50 +00002020 return Mnemonic;
2021}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002022
2023/// \brief Given a canonical mnemonic, determine if the instruction ever allows
2024/// inclusion of carry set or predication code operands.
2025//
2026// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00002027void ARMAsmParser::
2028GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2029 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002030 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2031 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2032 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2033 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002034 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002035 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2036 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002037 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00002038 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002039 CanAcceptCarrySet = true;
2040 } else {
2041 CanAcceptCarrySet = false;
2042 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002043
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002044 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2045 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2046 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2047 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00002048 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002049 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002050 CanAcceptPredicationCode = false;
2051 } else {
2052 CanAcceptPredicationCode = true;
2053 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002054
Evan Chengebdeeab2011-07-08 01:53:10 +00002055 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002056 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002057 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002058 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002059}
2060
2061/// Parse an arm instruction mnemonic followed by its operands.
2062bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2063 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2064 // Create the leading tokens for the mnemonic, split by '.' characters.
2065 size_t Start = 0, Next = Name.find('.');
2066 StringRef Head = Name.slice(Start, Next);
2067
Daniel Dunbar352e1482011-01-11 15:59:50 +00002068 // Split out the predication code and carry setting flag from the mnemonic.
2069 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002070 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002071 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002072 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
2073 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002074
Chris Lattner3a697562010-10-28 17:20:03 +00002075 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00002076
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002077 // Next, add the CCOut and ConditionCode operands, if needed.
2078 //
2079 // For mnemonics which can ever incorporate a carry setting bit or predication
2080 // code, our matching model involves us always generating CCOut and
2081 // ConditionCode operands to match the mnemonic "as written" and then we let
2082 // the matcher deal with finding the right instruction or generating an
2083 // appropriate error.
2084 bool CanAcceptCarrySet, CanAcceptPredicationCode;
2085 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
2086
Jim Grosbach33c16a22011-07-14 22:04:21 +00002087 // If we had a carry-set on an instruction that can't do that, issue an
2088 // error.
2089 if (!CanAcceptCarrySet && CarrySetting) {
2090 Parser.EatToEndOfStatement();
2091 return Error(NameLoc, "instruction '" + Head +
2092 "' can not set flags, but 's' suffix specified");
2093 }
2094
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002095 // Add the carry setting operand, if necessary.
2096 //
2097 // FIXME: It would be awesome if we could somehow invent a location such that
2098 // match errors on this operand would print a nice diagnostic about how the
2099 // 's' character in the mnemonic resulted in a CCOut operand.
Jim Grosbach33c16a22011-07-14 22:04:21 +00002100 if (CanAcceptCarrySet)
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002101 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2102 NameLoc));
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002103
2104 // Add the predication code operand, if necessary.
2105 if (CanAcceptPredicationCode) {
2106 Operands.push_back(ARMOperand::CreateCondCode(
2107 ARMCC::CondCodes(PredicationCode), NameLoc));
2108 } else {
2109 // This mnemonic can't ever accept a predication code, but the user wrote
2110 // one (or misspelled another mnemonic).
2111
2112 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002113 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002114
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002115 // Add the processor imod operand, if necessary.
2116 if (ProcessorIMod) {
2117 Operands.push_back(ARMOperand::CreateImm(
2118 MCConstantExpr::Create(ProcessorIMod, getContext()),
2119 NameLoc, NameLoc));
2120 } else {
2121 // This mnemonic can't ever accept a imod, but the user wrote
2122 // one (or misspelled another mnemonic).
2123
2124 // FIXME: Issue a nice error.
2125 }
2126
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002127 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002128 while (Next != StringRef::npos) {
2129 Start = Next;
2130 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002131 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002132
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002133 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002134 }
2135
2136 // Read the remaining operands.
2137 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002138 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002139 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002140 Parser.EatToEndOfStatement();
2141 return true;
2142 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002143
2144 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002145 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002146
2147 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002148 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002149 Parser.EatToEndOfStatement();
2150 return true;
2151 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002152 }
2153 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002154
Chris Lattnercbf8a982010-09-11 16:18:25 +00002155 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2156 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002157 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002158 }
Bill Wendling146018f2010-11-06 21:42:12 +00002159
Chris Lattner34e53142010-09-08 05:10:46 +00002160 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00002161 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002162}
2163
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002164bool ARMAsmParser::
2165MatchAndEmitInstruction(SMLoc IDLoc,
2166 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2167 MCStreamer &Out) {
2168 MCInst Inst;
2169 unsigned ErrorInfo;
Jim Grosbach5a187002011-07-19 18:32:48 +00002170 MatchResultTy MatchResult;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002171 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002172 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002173 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002174 Out.EmitInstruction(Inst);
2175 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002176 case Match_MissingFeature:
2177 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2178 return true;
2179 case Match_InvalidOperand: {
2180 SMLoc ErrorLoc = IDLoc;
2181 if (ErrorInfo != ~0U) {
2182 if (ErrorInfo >= Operands.size())
2183 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002184
Chris Lattnere73d4f82010-10-28 21:41:58 +00002185 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2186 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2187 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002188
Chris Lattnere73d4f82010-10-28 21:41:58 +00002189 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002190 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002191 case Match_MnemonicFail:
2192 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002193 case Match_ConversionFail:
2194 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002195 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002196
Eric Christopherc223e2b2010-10-29 09:26:59 +00002197 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002198 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002199}
2200
Kevin Enderby515d5092009-10-15 20:48:48 +00002201/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002202bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2203 StringRef IDVal = DirectiveID.getIdentifier();
2204 if (IDVal == ".word")
2205 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002206 else if (IDVal == ".thumb")
2207 return ParseDirectiveThumb(DirectiveID.getLoc());
2208 else if (IDVal == ".thumb_func")
2209 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2210 else if (IDVal == ".code")
2211 return ParseDirectiveCode(DirectiveID.getLoc());
2212 else if (IDVal == ".syntax")
2213 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002214 return true;
2215}
2216
2217/// ParseDirectiveWord
2218/// ::= .word [ expression (, expression)* ]
2219bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2220 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2221 for (;;) {
2222 const MCExpr *Value;
2223 if (getParser().ParseExpression(Value))
2224 return true;
2225
Chris Lattneraaec2052010-01-19 19:46:13 +00002226 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002227
2228 if (getLexer().is(AsmToken::EndOfStatement))
2229 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002230
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002231 // FIXME: Improve diagnostic.
2232 if (getLexer().isNot(AsmToken::Comma))
2233 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002234 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002235 }
2236 }
2237
Sean Callananb9a25b72010-01-19 20:27:46 +00002238 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002239 return false;
2240}
2241
Kevin Enderby515d5092009-10-15 20:48:48 +00002242/// ParseDirectiveThumb
2243/// ::= .thumb
2244bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2245 if (getLexer().isNot(AsmToken::EndOfStatement))
2246 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002247 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002248
2249 // TODO: set thumb mode
2250 // TODO: tell the MC streamer the mode
2251 // getParser().getStreamer().Emit???();
2252 return false;
2253}
2254
2255/// ParseDirectiveThumbFunc
2256/// ::= .thumbfunc symbol_name
2257bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002258 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2259 bool isMachO = MAI.hasSubsectionsViaSymbols();
2260 StringRef Name;
2261
2262 // Darwin asm has function name after .thumb_func direction
2263 // ELF doesn't
2264 if (isMachO) {
2265 const AsmToken &Tok = Parser.getTok();
2266 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2267 return Error(L, "unexpected token in .thumb_func directive");
2268 Name = Tok.getString();
2269 Parser.Lex(); // Consume the identifier token.
2270 }
2271
Kevin Enderby515d5092009-10-15 20:48:48 +00002272 if (getLexer().isNot(AsmToken::EndOfStatement))
2273 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002274 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002275
Rafael Espindola64695402011-05-16 16:17:21 +00002276 // FIXME: assuming function name will be the line following .thumb_func
2277 if (!isMachO) {
2278 Name = Parser.getTok().getString();
2279 }
2280
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002281 // Mark symbol as a thumb symbol.
2282 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2283 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002284 return false;
2285}
2286
2287/// ParseDirectiveSyntax
2288/// ::= .syntax unified | divided
2289bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002290 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002291 if (Tok.isNot(AsmToken::Identifier))
2292 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002293 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002294 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002295 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002296 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002297 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002298 else
2299 return Error(L, "unrecognized syntax mode in .syntax directive");
2300
2301 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002302 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002303 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002304
2305 // TODO tell the MC streamer the mode
2306 // getParser().getStreamer().Emit???();
2307 return false;
2308}
2309
2310/// ParseDirectiveCode
2311/// ::= .code 16 | 32
2312bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002313 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002314 if (Tok.isNot(AsmToken::Integer))
2315 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002316 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002317 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002318 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002319 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002320 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002321 else
2322 return Error(L, "invalid operand to .code directive");
2323
2324 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002325 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002326 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002327
Evan Cheng32869202011-07-08 22:36:29 +00002328 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002329 if (!isThumb())
2330 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002331 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002332 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002333 if (isThumb())
2334 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002335 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002336 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002337
Kevin Enderby515d5092009-10-15 20:48:48 +00002338 return false;
2339}
2340
Sean Callanan90b70972010-04-07 20:29:34 +00002341extern "C" void LLVMInitializeARMAsmLexer();
2342
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002343/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002344extern "C" void LLVMInitializeARMAsmParser() {
2345 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2346 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002347 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002348}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002349
Chris Lattner0692ee62010-09-06 19:11:01 +00002350#define GET_REGISTER_MATCHER
2351#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002352#include "ARMGenAsmMatcher.inc"