blob: bdcdd7966ebd80da8d660148952c704bd3cb066f [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 Grosbach6b8f1e32011-06-27 23:54:06 +0000410 bool isT2SOImm() 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::getT2SOImmVal(Value) != -1;
417 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000418 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000419 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000420 bool isDPRRegList() const { return Kind == DPRRegisterList; }
421 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000422 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000423 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000424 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000425 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000426 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000427 bool isMemMode2() const {
428 if (getMemAddrMode() != ARMII::AddrMode2)
429 return false;
430
431 if (getMemOffsetIsReg())
432 return true;
433
434 if (getMemNegative() &&
435 !(getMemPostindexed() || getMemPreindexed()))
436 return false;
437
438 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
439 if (!CE) return false;
440 int64_t Value = CE->getValue();
441
442 // The offset must be in the range 0-4095 (imm12).
443 if (Value > 4095 || Value < -4095)
444 return false;
445
446 return true;
447 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000448 bool isMemMode3() const {
449 if (getMemAddrMode() != ARMII::AddrMode3)
450 return false;
451
452 if (getMemOffsetIsReg()) {
453 if (getMemOffsetRegShifted())
454 return false; // No shift with offset reg allowed
455 return true;
456 }
457
458 if (getMemNegative() &&
459 !(getMemPostindexed() || getMemPreindexed()))
460 return false;
461
462 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
463 if (!CE) return false;
464 int64_t Value = CE->getValue();
465
466 // The offset must be in the range 0-255 (imm8).
467 if (Value > 255 || Value < -255)
468 return false;
469
470 return true;
471 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000472 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000473 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
474 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000475 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000476
Daniel Dunbar4b462672011-01-18 05:55:27 +0000477 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000478 if (!CE) return false;
479
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000480 // The offset must be a multiple of 4 in the range 0-1020.
481 int64_t Value = CE->getValue();
482 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
483 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000484 bool isMemMode7() const {
485 if (!isMemory() ||
486 getMemPreindexed() ||
487 getMemPostindexed() ||
488 getMemOffsetIsReg() ||
489 getMemNegative() ||
490 getMemWriteback())
491 return false;
492
493 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
494 if (!CE) return false;
495
496 if (CE->getValue())
497 return false;
498
499 return true;
500 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000501 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000502 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000503 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000504 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000505 }
506 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000507 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000508 return false;
509
Daniel Dunbar4b462672011-01-18 05:55:27 +0000510 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000511 if (!CE) return false;
512
513 // The offset must be a multiple of 4 in the range 0-124.
514 uint64_t Value = CE->getValue();
515 return ((Value & 0x3) == 0 && Value <= 124);
516 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000517 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000518 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000519
520 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000521 // Add as immediates when possible. Null MCExpr = 0.
522 if (Expr == 0)
523 Inst.addOperand(MCOperand::CreateImm(0));
524 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000525 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
526 else
527 Inst.addOperand(MCOperand::CreateExpr(Expr));
528 }
529
Daniel Dunbar8462b302010-08-11 06:36:53 +0000530 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000531 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000532 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000533 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
534 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000535 }
536
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000537 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
538 assert(N == 1 && "Invalid number of operands!");
539 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
540 }
541
542 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
543 assert(N == 1 && "Invalid number of operands!");
544 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
545 }
546
Jim Grosbachd67641b2010-12-06 18:21:12 +0000547 void addCCOutOperands(MCInst &Inst, unsigned N) const {
548 assert(N == 1 && "Invalid number of operands!");
549 Inst.addOperand(MCOperand::CreateReg(getReg()));
550 }
551
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000552 void addRegOperands(MCInst &Inst, unsigned N) const {
553 assert(N == 1 && "Invalid number of operands!");
554 Inst.addOperand(MCOperand::CreateReg(getReg()));
555 }
556
Jim Grosbache8606dc2011-07-13 17:50:29 +0000557 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
558 assert(N == 3 && "Invalid number of operands!");
559 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
560 assert((ShiftedReg.ShiftReg == 0 ||
561 ARM_AM::getSORegOffset(ShiftedReg.ShiftImm) == 0) &&
562 "Invalid shifted register operand!");
563 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
564 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
565 Inst.addOperand(MCOperand::CreateImm(
566 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
567 }
568
Owen Anderson00828302011-03-18 22:50:18 +0000569 void addShifterOperands(MCInst &Inst, unsigned N) const {
570 assert(N == 1 && "Invalid number of operands!");
571 Inst.addOperand(MCOperand::CreateImm(
572 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
573 }
574
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000575 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000576 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000577 const SmallVectorImpl<unsigned> &RegList = getRegList();
578 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000579 I = RegList.begin(), E = RegList.end(); I != E; ++I)
580 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000581 }
582
Bill Wendling0f630752010-11-17 04:32:08 +0000583 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
584 addRegListOperands(Inst, N);
585 }
586
587 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
588 addRegListOperands(Inst, N);
589 }
590
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000591 void addImmOperands(MCInst &Inst, unsigned N) const {
592 assert(N == 1 && "Invalid number of operands!");
593 addExpr(Inst, getImm());
594 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000595
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000596 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
597 assert(N == 1 && "Invalid number of operands!");
598 addExpr(Inst, getImm());
599 }
600
Jim Grosbach83ab0702011-07-13 22:01:08 +0000601 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
602 assert(N == 1 && "Invalid number of operands!");
603 addExpr(Inst, getImm());
604 }
605
606 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
607 assert(N == 1 && "Invalid number of operands!");
608 addExpr(Inst, getImm());
609 }
610
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000611 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
612 assert(N == 1 && "Invalid number of operands!");
613 addExpr(Inst, getImm());
614 }
615
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000616 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
617 assert(N == 1 && "Invalid number of operands!");
618 addExpr(Inst, getImm());
619 }
620
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000621 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
622 assert(N == 1 && "Invalid number of operands!");
623 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
624 }
625
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000626 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
627 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
628 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
629
630 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000631 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000632 assert((CE || CE->getValue() == 0) &&
633 "No offset operand support in mode 7");
634 }
635
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000636 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
637 assert(isMemMode2() && "Invalid mode or number of operands!");
638 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
639 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
640
641 if (getMemOffsetIsReg()) {
642 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
643
644 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
645 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
646 int64_t ShiftAmount = 0;
647
648 if (getMemOffsetRegShifted()) {
649 ShOpc = getMemShiftType();
650 const MCConstantExpr *CE =
651 dyn_cast<MCConstantExpr>(getMemShiftAmount());
652 ShiftAmount = CE->getValue();
653 }
654
655 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
656 ShOpc, IdxMode)));
657 return;
658 }
659
660 // Create a operand placeholder to always yield the same number of operands.
661 Inst.addOperand(MCOperand::CreateReg(0));
662
663 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
664 // the difference?
665 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
666 assert(CE && "Non-constant mode 2 offset operand!");
667 int64_t Offset = CE->getValue();
668
669 if (Offset >= 0)
670 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
671 Offset, ARM_AM::no_shift, IdxMode)));
672 else
673 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
674 -Offset, ARM_AM::no_shift, IdxMode)));
675 }
676
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000677 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
678 assert(isMemMode3() && "Invalid mode or number of operands!");
679 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
680 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
681
682 if (getMemOffsetIsReg()) {
683 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
684
685 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
686 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
687 IdxMode)));
688 return;
689 }
690
691 // Create a operand placeholder to always yield the same number of operands.
692 Inst.addOperand(MCOperand::CreateReg(0));
693
694 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
695 // the difference?
696 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
697 assert(CE && "Non-constant mode 3 offset operand!");
698 int64_t Offset = CE->getValue();
699
700 if (Offset >= 0)
701 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
702 Offset, IdxMode)));
703 else
704 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
705 -Offset, IdxMode)));
706 }
707
Chris Lattner14b93852010-10-29 00:27:31 +0000708 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
709 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000710
Daniel Dunbar4b462672011-01-18 05:55:27 +0000711 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
712 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000713
Jim Grosbach80eb2332010-10-29 17:41:25 +0000714 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
715 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000716 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000717 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000718
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000719 // The MCInst offset operand doesn't include the low two bits (like
720 // the instruction encoding).
721 int64_t Offset = CE->getValue() / 4;
722 if (Offset >= 0)
723 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
724 Offset)));
725 else
726 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
727 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000728 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000729
Bill Wendlingf4caf692010-12-14 03:36:38 +0000730 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
731 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000732 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
733 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000734 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000735
Bill Wendlingf4caf692010-12-14 03:36:38 +0000736 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
737 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000738 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
739 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000740 assert(CE && "Non-constant mode offset operand!");
741 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000742 }
743
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000744 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
745 assert(N == 1 && "Invalid number of operands!");
746 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
747 }
748
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000749 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
750 assert(N == 1 && "Invalid number of operands!");
751 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
752 }
753
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000754 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000755
Chris Lattner3a697562010-10-28 17:20:03 +0000756 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
757 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000758 Op->CC.Val = CC;
759 Op->StartLoc = S;
760 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000761 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000762 }
763
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000764 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
765 ARMOperand *Op = new ARMOperand(CoprocNum);
766 Op->Cop.Val = CopVal;
767 Op->StartLoc = S;
768 Op->EndLoc = S;
769 return Op;
770 }
771
772 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
773 ARMOperand *Op = new ARMOperand(CoprocReg);
774 Op->Cop.Val = CopVal;
775 Op->StartLoc = S;
776 Op->EndLoc = S;
777 return Op;
778 }
779
Jim Grosbachd67641b2010-12-06 18:21:12 +0000780 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
781 ARMOperand *Op = new ARMOperand(CCOut);
782 Op->Reg.RegNum = RegNum;
783 Op->StartLoc = S;
784 Op->EndLoc = S;
785 return Op;
786 }
787
Chris Lattner3a697562010-10-28 17:20:03 +0000788 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
789 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000790 Op->Tok.Data = Str.data();
791 Op->Tok.Length = Str.size();
792 Op->StartLoc = S;
793 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000794 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000795 }
796
Bill Wendling50d0f582010-11-18 23:43:05 +0000797 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000798 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000799 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000800 Op->StartLoc = S;
801 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000802 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000803 }
804
Jim Grosbache8606dc2011-07-13 17:50:29 +0000805 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
806 unsigned SrcReg,
807 unsigned ShiftReg,
808 unsigned ShiftImm,
809 SMLoc S, SMLoc E) {
810 ARMOperand *Op = new ARMOperand(ShiftedRegister);
811 Op->ShiftedReg.ShiftTy = ShTy;
812 Op->ShiftedReg.SrcReg = SrcReg;
813 Op->ShiftedReg.ShiftReg = ShiftReg;
814 Op->ShiftedReg.ShiftImm = ShiftImm;
815 Op->StartLoc = S;
816 Op->EndLoc = E;
817 return Op;
818 }
819
Owen Anderson00828302011-03-18 22:50:18 +0000820 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
821 SMLoc S, SMLoc E) {
822 ARMOperand *Op = new ARMOperand(Shifter);
823 Op->Shift.ShiftTy = ShTy;
824 Op->StartLoc = S;
825 Op->EndLoc = E;
826 return Op;
827 }
828
Bill Wendling7729e062010-11-09 22:44:22 +0000829 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000830 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000831 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000832 KindTy Kind = RegisterList;
833
834 if (ARM::DPRRegClass.contains(Regs.front().first))
835 Kind = DPRRegisterList;
836 else if (ARM::SPRRegClass.contains(Regs.front().first))
837 Kind = SPRRegisterList;
838
839 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000840 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000841 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000842 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000843 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000844 Op->StartLoc = StartLoc;
845 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000846 return Op;
847 }
848
Chris Lattner3a697562010-10-28 17:20:03 +0000849 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
850 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000851 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000852 Op->StartLoc = S;
853 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000854 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000855 }
856
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000857 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
858 bool OffsetIsReg, const MCExpr *Offset,
859 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000860 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000861 const MCExpr *ShiftAmount, bool Preindexed,
862 bool Postindexed, bool Negative, bool Writeback,
863 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000864 assert((OffsetRegNum == -1 || OffsetIsReg) &&
865 "OffsetRegNum must imply OffsetIsReg!");
866 assert((!OffsetRegShifted || OffsetIsReg) &&
867 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000868 assert((Offset || OffsetIsReg) &&
869 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000870 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
871 "Cannot have shift amount without shifted register offset!");
872 assert((!Offset || !OffsetIsReg) &&
873 "Cannot have expression offset and register offset!");
874
Chris Lattner3a697562010-10-28 17:20:03 +0000875 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000876 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000877 Op->Mem.BaseRegNum = BaseRegNum;
878 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000879 if (OffsetIsReg)
880 Op->Mem.Offset.RegNum = OffsetRegNum;
881 else
882 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000883 Op->Mem.OffsetRegShifted = OffsetRegShifted;
884 Op->Mem.ShiftType = ShiftType;
885 Op->Mem.ShiftAmount = ShiftAmount;
886 Op->Mem.Preindexed = Preindexed;
887 Op->Mem.Postindexed = Postindexed;
888 Op->Mem.Negative = Negative;
889 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000890
Sean Callanan76264762010-04-02 22:27:05 +0000891 Op->StartLoc = S;
892 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000893 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000894 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000895
896 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
897 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
898 Op->MBOpt.Val = Opt;
899 Op->StartLoc = S;
900 Op->EndLoc = S;
901 return Op;
902 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000903
904 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
905 ARMOperand *Op = new ARMOperand(ProcIFlags);
906 Op->IFlags.Val = IFlags;
907 Op->StartLoc = S;
908 Op->EndLoc = S;
909 return Op;
910 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000911
912 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
913 ARMOperand *Op = new ARMOperand(MSRMask);
914 Op->MMask.Val = MMask;
915 Op->StartLoc = S;
916 Op->EndLoc = S;
917 return Op;
918 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000919};
920
921} // end anonymous namespace.
922
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000923void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000924 switch (Kind) {
925 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000926 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000927 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000928 case CCOut:
929 OS << "<ccout " << getReg() << ">";
930 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000931 case CoprocNum:
932 OS << "<coprocessor number: " << getCoproc() << ">";
933 break;
934 case CoprocReg:
935 OS << "<coprocessor register: " << getCoproc() << ">";
936 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000937 case MSRMask:
938 OS << "<mask: " << getMSRMask() << ">";
939 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000940 case Immediate:
941 getImm()->print(OS);
942 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000943 case MemBarrierOpt:
944 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
945 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000946 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000947 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000948 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
949 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000950 if (getMemOffsetIsReg()) {
951 OS << " offset:<register " << getMemOffsetRegNum();
952 if (getMemOffsetRegShifted()) {
953 OS << " offset-shift-type:" << getMemShiftType();
954 OS << " offset-shift-amount:" << *getMemShiftAmount();
955 }
956 } else {
957 OS << " offset:" << *getMemOffset();
958 }
959 if (getMemOffsetIsReg())
960 OS << " (offset-is-reg)";
961 if (getMemPreindexed())
962 OS << " (pre-indexed)";
963 if (getMemPostindexed())
964 OS << " (post-indexed)";
965 if (getMemNegative())
966 OS << " (negative)";
967 if (getMemWriteback())
968 OS << " (writeback)";
969 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000970 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000971 case ProcIFlags: {
972 OS << "<ARM_PROC::";
973 unsigned IFlags = getProcIFlags();
974 for (int i=2; i >= 0; --i)
975 if (IFlags & (1 << i))
976 OS << ARM_PROC::IFlagsToString(1 << i);
977 OS << ">";
978 break;
979 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000980 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000981 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000982 break;
Owen Anderson00828302011-03-18 22:50:18 +0000983 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +0000984 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
985 break;
986 case ShiftedRegister:
987 OS << "<so_reg"
988 << ShiftedReg.SrcReg
989 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
990 << ", " << ShiftedReg.ShiftReg << ", "
991 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
992 << ">";
Owen Anderson00828302011-03-18 22:50:18 +0000993 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000994 case RegisterList:
995 case DPRRegisterList:
996 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000997 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000998
Bill Wendling5fa22a12010-11-09 23:28:44 +0000999 const SmallVectorImpl<unsigned> &RegList = getRegList();
1000 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001001 I = RegList.begin(), E = RegList.end(); I != E; ) {
1002 OS << *I;
1003 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001004 }
1005
1006 OS << ">";
1007 break;
1008 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001009 case Token:
1010 OS << "'" << getToken() << "'";
1011 break;
1012 }
1013}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001014
1015/// @name Auto-generated Match Functions
1016/// {
1017
1018static unsigned MatchRegisterName(StringRef Name);
1019
1020/// }
1021
Bob Wilson69df7232011-02-03 21:46:10 +00001022bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1023 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +00001024 RegNo = TryParseRegister();
1025
1026 return (RegNo == (unsigned)-1);
1027}
1028
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001029/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00001030/// and if it is a register name the token is eaten and the register number is
1031/// returned. Otherwise return -1.
1032///
1033int ARMAsmParser::TryParseRegister() {
1034 const AsmToken &Tok = Parser.getTok();
1035 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001036
Chris Lattnere5658fa2010-10-30 04:09:10 +00001037 // FIXME: Validate register for the current architecture; we have to do
1038 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001039 std::string upperCase = Tok.getString().str();
1040 std::string lowerCase = LowercaseString(upperCase);
1041 unsigned RegNum = MatchRegisterName(lowerCase);
1042 if (!RegNum) {
1043 RegNum = StringSwitch<unsigned>(lowerCase)
1044 .Case("r13", ARM::SP)
1045 .Case("r14", ARM::LR)
1046 .Case("r15", ARM::PC)
1047 .Case("ip", ARM::R12)
1048 .Default(0);
1049 }
1050 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001051
Chris Lattnere5658fa2010-10-30 04:09:10 +00001052 Parser.Lex(); // Eat identifier token.
1053 return RegNum;
1054}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001055
Jim Grosbach19906722011-07-13 18:49:30 +00001056// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1057// If a recoverable error occurs, return 1. If an irrecoverable error
1058// occurs, return -1. An irrecoverable error is one where tokens have been
1059// consumed in the process of trying to parse the shifter (i.e., when it is
1060// indeed a shifter operand, but malformed).
1061int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001062 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1063 SMLoc S = Parser.getTok().getLoc();
1064 const AsmToken &Tok = Parser.getTok();
1065 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1066
1067 std::string upperCase = Tok.getString().str();
1068 std::string lowerCase = LowercaseString(upperCase);
1069 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1070 .Case("lsl", ARM_AM::lsl)
1071 .Case("lsr", ARM_AM::lsr)
1072 .Case("asr", ARM_AM::asr)
1073 .Case("ror", ARM_AM::ror)
1074 .Case("rrx", ARM_AM::rrx)
1075 .Default(ARM_AM::no_shift);
1076
1077 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001078 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001079
Jim Grosbache8606dc2011-07-13 17:50:29 +00001080 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001081
Jim Grosbache8606dc2011-07-13 17:50:29 +00001082 // The source register for the shift has already been added to the
1083 // operand list, so we need to pop it off and combine it into the shifted
1084 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00001085 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00001086 if (!PrevOp->isReg())
1087 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1088 int SrcReg = PrevOp->getReg();
1089 int64_t Imm = 0;
1090 int ShiftReg = 0;
1091 if (ShiftTy == ARM_AM::rrx) {
1092 // RRX Doesn't have an explicit shift amount. The encoder expects
1093 // the shift register to be the same as the source register. Seems odd,
1094 // but OK.
1095 ShiftReg = SrcReg;
1096 } else {
1097 // Figure out if this is shifted by a constant or a register (for non-RRX).
1098 if (Parser.getTok().is(AsmToken::Hash)) {
1099 Parser.Lex(); // Eat hash.
1100 SMLoc ImmLoc = Parser.getTok().getLoc();
1101 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001102 if (getParser().ParseExpression(ShiftExpr)) {
1103 Error(ImmLoc, "invalid immediate shift value");
1104 return -1;
1105 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001106 // The expression must be evaluatable as an immediate.
1107 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001108 if (!CE) {
1109 Error(ImmLoc, "invalid immediate shift value");
1110 return -1;
1111 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001112 // Range check the immediate.
1113 // lsl, ror: 0 <= imm <= 31
1114 // lsr, asr: 0 <= imm <= 32
1115 Imm = CE->getValue();
1116 if (Imm < 0 ||
1117 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1118 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001119 Error(ImmLoc, "immediate shift value out of range");
1120 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001121 }
1122 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1123 ShiftReg = TryParseRegister();
1124 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001125 if (ShiftReg == -1) {
1126 Error (L, "expected immediate or register in shift operand");
1127 return -1;
1128 }
1129 } else {
1130 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001131 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001132 return -1;
1133 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001134 }
1135
Jim Grosbache8606dc2011-07-13 17:50:29 +00001136 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1137 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001138 S, Parser.getTok().getLoc()));
1139
Jim Grosbach19906722011-07-13 18:49:30 +00001140 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001141}
1142
1143
Bill Wendling50d0f582010-11-18 23:43:05 +00001144/// Try to parse a register name. The token must be an Identifier when called.
1145/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1146/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001147///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001148/// TODO this is likely to change to allow different register types and or to
1149/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001150bool ARMAsmParser::
1151TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001152 SMLoc S = Parser.getTok().getLoc();
1153 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001154 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001155 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001156
Bill Wendling50d0f582010-11-18 23:43:05 +00001157 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001158
Chris Lattnere5658fa2010-10-30 04:09:10 +00001159 const AsmToken &ExclaimTok = Parser.getTok();
1160 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001161 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1162 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001163 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001164 }
1165
Bill Wendling50d0f582010-11-18 23:43:05 +00001166 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001167}
1168
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001169/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1170/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1171/// "c5", ...
1172static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001173 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1174 // but efficient.
1175 switch (Name.size()) {
1176 default: break;
1177 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001178 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001179 return -1;
1180 switch (Name[1]) {
1181 default: return -1;
1182 case '0': return 0;
1183 case '1': return 1;
1184 case '2': return 2;
1185 case '3': return 3;
1186 case '4': return 4;
1187 case '5': return 5;
1188 case '6': return 6;
1189 case '7': return 7;
1190 case '8': return 8;
1191 case '9': return 9;
1192 }
1193 break;
1194 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001195 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001196 return -1;
1197 switch (Name[2]) {
1198 default: return -1;
1199 case '0': return 10;
1200 case '1': return 11;
1201 case '2': return 12;
1202 case '3': return 13;
1203 case '4': return 14;
1204 case '5': return 15;
1205 }
1206 break;
1207 }
1208
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001209 return -1;
1210}
1211
Jim Grosbachf922c472011-02-12 01:34:40 +00001212/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001213/// token must be an Identifier when called, and if it is a coprocessor
1214/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001215ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1216tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001217 SMLoc S = Parser.getTok().getLoc();
1218 const AsmToken &Tok = Parser.getTok();
1219 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1220
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001221 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001222 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001223 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001224
1225 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001226 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001227 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001228}
1229
Jim Grosbachf922c472011-02-12 01:34:40 +00001230/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001231/// token must be an Identifier when called, and if it is a coprocessor
1232/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001233ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1234tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001235 SMLoc S = Parser.getTok().getLoc();
1236 const AsmToken &Tok = Parser.getTok();
1237 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1238
1239 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1240 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001241 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001242
1243 Parser.Lex(); // Eat identifier token.
1244 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001245 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001246}
1247
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001248/// Parse a register list, return it if successful else return null. The first
1249/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001250bool ARMAsmParser::
1251ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001252 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001253 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001254 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001255
Bill Wendling7729e062010-11-09 22:44:22 +00001256 // Read the rest of the registers in the list.
1257 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001258 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001259
Bill Wendling7729e062010-11-09 22:44:22 +00001260 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001261 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001262 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001263
Sean Callanan18b83232010-01-19 21:44:56 +00001264 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001265 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001266 if (RegTok.isNot(AsmToken::Identifier)) {
1267 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001268 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001269 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001270
Bill Wendling1d6a2652010-11-06 10:40:24 +00001271 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001272 if (RegNum == -1) {
1273 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001274 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001275 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001276
Bill Wendlinge7176102010-11-06 22:36:58 +00001277 if (IsRange) {
1278 int Reg = PrevRegNum;
1279 do {
1280 ++Reg;
1281 Registers.push_back(std::make_pair(Reg, RegLoc));
1282 } while (Reg != RegNum);
1283 } else {
1284 Registers.push_back(std::make_pair(RegNum, RegLoc));
1285 }
1286
1287 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001288 } while (Parser.getTok().is(AsmToken::Comma) ||
1289 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001290
1291 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001292 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001293 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1294 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001295 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001296 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001297
Bill Wendlinge7176102010-11-06 22:36:58 +00001298 SMLoc E = RCurlyTok.getLoc();
1299 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001300
Bill Wendlinge7176102010-11-06 22:36:58 +00001301 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001302 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001303 RI = Registers.begin(), RE = Registers.end();
1304
Bill Wendling7caebff2011-01-12 21:20:59 +00001305 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001306 bool EmittedWarning = false;
1307
Bill Wendling7caebff2011-01-12 21:20:59 +00001308 DenseMap<unsigned, bool> RegMap;
1309 RegMap[HighRegNum] = true;
1310
Bill Wendlinge7176102010-11-06 22:36:58 +00001311 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001312 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001313 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001314
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001315 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001316 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001317 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001318 }
1319
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001320 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001321 Warning(RegInfo.second,
1322 "register not in ascending order in register list");
1323
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001324 RegMap[Reg] = true;
1325 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001326 }
1327
Bill Wendling50d0f582010-11-18 23:43:05 +00001328 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1329 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001330}
1331
Jim Grosbachf922c472011-02-12 01:34:40 +00001332/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1333ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1334tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001335 SMLoc S = Parser.getTok().getLoc();
1336 const AsmToken &Tok = Parser.getTok();
1337 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1338 StringRef OptStr = Tok.getString();
1339
1340 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1341 .Case("sy", ARM_MB::SY)
1342 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001343 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001344 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001345 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001346 .Case("ishst", ARM_MB::ISHST)
1347 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001348 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001349 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001350 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001351 .Case("osh", ARM_MB::OSH)
1352 .Case("oshst", ARM_MB::OSHST)
1353 .Default(~0U);
1354
1355 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001356 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001357
1358 Parser.Lex(); // Eat identifier token.
1359 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001360 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001361}
1362
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001363/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001364ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1365tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1366 SMLoc S = Parser.getTok().getLoc();
1367 const AsmToken &Tok = Parser.getTok();
1368 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1369 StringRef IFlagsStr = Tok.getString();
1370
1371 unsigned IFlags = 0;
1372 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1373 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1374 .Case("a", ARM_PROC::A)
1375 .Case("i", ARM_PROC::I)
1376 .Case("f", ARM_PROC::F)
1377 .Default(~0U);
1378
1379 // If some specific iflag is already set, it means that some letter is
1380 // present more than once, this is not acceptable.
1381 if (Flag == ~0U || (IFlags & Flag))
1382 return MatchOperand_NoMatch;
1383
1384 IFlags |= Flag;
1385 }
1386
1387 Parser.Lex(); // Eat identifier token.
1388 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1389 return MatchOperand_Success;
1390}
1391
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001392/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1393ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1394tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1395 SMLoc S = Parser.getTok().getLoc();
1396 const AsmToken &Tok = Parser.getTok();
1397 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1398 StringRef Mask = Tok.getString();
1399
1400 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1401 size_t Start = 0, Next = Mask.find('_');
1402 StringRef Flags = "";
1403 StringRef SpecReg = Mask.slice(Start, Next);
1404 if (Next != StringRef::npos)
1405 Flags = Mask.slice(Next+1, Mask.size());
1406
1407 // FlagsVal contains the complete mask:
1408 // 3-0: Mask
1409 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1410 unsigned FlagsVal = 0;
1411
1412 if (SpecReg == "apsr") {
1413 FlagsVal = StringSwitch<unsigned>(Flags)
1414 .Case("nzcvq", 0x8) // same as CPSR_c
1415 .Case("g", 0x4) // same as CPSR_s
1416 .Case("nzcvqg", 0xc) // same as CPSR_fs
1417 .Default(~0U);
1418
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001419 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001420 if (!Flags.empty())
1421 return MatchOperand_NoMatch;
1422 else
1423 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001424 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001425 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001426 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1427 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001428 for (int i = 0, e = Flags.size(); i != e; ++i) {
1429 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1430 .Case("c", 1)
1431 .Case("x", 2)
1432 .Case("s", 4)
1433 .Case("f", 8)
1434 .Default(~0U);
1435
1436 // If some specific flag is already set, it means that some letter is
1437 // present more than once, this is not acceptable.
1438 if (FlagsVal == ~0U || (FlagsVal & Flag))
1439 return MatchOperand_NoMatch;
1440 FlagsVal |= Flag;
1441 }
1442 } else // No match for special register.
1443 return MatchOperand_NoMatch;
1444
1445 // Special register without flags are equivalent to "fc" flags.
1446 if (!FlagsVal)
1447 FlagsVal = 0x9;
1448
1449 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1450 if (SpecReg == "spsr")
1451 FlagsVal |= 16;
1452
1453 Parser.Lex(); // Eat identifier token.
1454 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1455 return MatchOperand_Success;
1456}
1457
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001458/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1459ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1460tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001461 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001462
1463 if (ParseMemory(Operands, ARMII::AddrMode2))
1464 return MatchOperand_NoMatch;
1465
1466 return MatchOperand_Success;
1467}
1468
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001469/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1470ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1471tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1472 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1473
1474 if (ParseMemory(Operands, ARMII::AddrMode3))
1475 return MatchOperand_NoMatch;
1476
1477 return MatchOperand_Success;
1478}
1479
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001480/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1481/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1482/// when they refer multiple MIOperands inside a single one.
1483bool ARMAsmParser::
1484CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1485 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1486 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1487
1488 // Create a writeback register dummy placeholder.
1489 Inst.addOperand(MCOperand::CreateImm(0));
1490
1491 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1492 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1493 return true;
1494}
1495
1496/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1497/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1498/// when they refer multiple MIOperands inside a single one.
1499bool ARMAsmParser::
1500CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1501 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1502 // Create a writeback register dummy placeholder.
1503 Inst.addOperand(MCOperand::CreateImm(0));
1504 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1505 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1506 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1507 return true;
1508}
1509
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001510/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1511/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1512/// when they refer multiple MIOperands inside a single one.
1513bool ARMAsmParser::
1514CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1515 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1516 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1517
1518 // Create a writeback register dummy placeholder.
1519 Inst.addOperand(MCOperand::CreateImm(0));
1520
1521 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1522 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1523 return true;
1524}
1525
1526/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1527/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1528/// when they refer multiple MIOperands inside a single one.
1529bool ARMAsmParser::
1530CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1531 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1532 // Create a writeback register dummy placeholder.
1533 Inst.addOperand(MCOperand::CreateImm(0));
1534 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1535 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1536 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1537 return true;
1538}
1539
Bill Wendlinge7176102010-11-06 22:36:58 +00001540/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001541/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001542///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001543/// TODO Only preindexing and postindexing addressing are started, unindexed
1544/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001545bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001546ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1547 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001548 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001549 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001550 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001551 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001552 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001553
Sean Callanan18b83232010-01-19 21:44:56 +00001554 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001555 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1556 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001557 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001558 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001559 int BaseRegNum = TryParseRegister();
1560 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001561 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001562 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001563 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001564
Daniel Dunbar05710932011-01-18 05:34:17 +00001565 // The next token must either be a comma or a closing bracket.
1566 const AsmToken &Tok = Parser.getTok();
1567 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1568 return true;
1569
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001570 bool Preindexed = false;
1571 bool Postindexed = false;
1572 bool OffsetIsReg = false;
1573 bool Negative = false;
1574 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001575 ARMOperand *WBOp = 0;
1576 int OffsetRegNum = -1;
1577 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001578 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001579 const MCExpr *ShiftAmount = 0;
1580 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001581
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001582 // First look for preindexed address forms, that is after the "[Rn" we now
1583 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001584 if (Tok.is(AsmToken::Comma)) {
1585 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001586 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001587
Chris Lattner550276e2010-10-28 20:52:15 +00001588 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1589 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001590 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001591 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001592 if (RBracTok.isNot(AsmToken::RBrac)) {
1593 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001594 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001595 }
Sean Callanan76264762010-04-02 22:27:05 +00001596 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001597 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001598
Sean Callanan18b83232010-01-19 21:44:56 +00001599 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001600 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001601 // None of addrmode3 instruction uses "!"
1602 if (AddrMode == ARMII::AddrMode3)
1603 return true;
1604
Bill Wendling50d0f582010-11-18 23:43:05 +00001605 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1606 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001607 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001608 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001609 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1610 if (AddrMode == ARMII::AddrMode2)
1611 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001612 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001613 } else {
1614 // The "[Rn" we have so far was not followed by a comma.
1615
Jim Grosbach80eb2332010-10-29 17:41:25 +00001616 // If there's anything other than the right brace, this is a post indexing
1617 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001618 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001619 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001620
Sean Callanan18b83232010-01-19 21:44:56 +00001621 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001622
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001623 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001624 Postindexed = true;
1625 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001626
Chris Lattner550276e2010-10-28 20:52:15 +00001627 if (NextTok.isNot(AsmToken::Comma)) {
1628 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001629 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001630 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001631
Sean Callananb9a25b72010-01-19 20:27:46 +00001632 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001633
Chris Lattner550276e2010-10-28 20:52:15 +00001634 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001635 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001636 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001637 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001638 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001639 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001640
1641 // Force Offset to exist if used.
1642 if (!OffsetIsReg) {
1643 if (!Offset)
1644 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001645 } else {
1646 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1647 Error(E, "shift amount not supported");
1648 return true;
1649 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001650 }
1651
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001652 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1653 Offset, OffsetRegNum, OffsetRegShifted,
1654 ShiftType, ShiftAmount, Preindexed,
1655 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001656 if (WBOp)
1657 Operands.push_back(WBOp);
1658
1659 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001660}
1661
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001662/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1663/// we will parse the following (were +/- means that a plus or minus is
1664/// optional):
1665/// +/-Rm
1666/// +/-Rm, shift
1667/// #offset
1668/// we return false on success or an error otherwise.
1669bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001670 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001671 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001672 const MCExpr *&ShiftAmount,
1673 const MCExpr *&Offset,
1674 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001675 int &OffsetRegNum,
1676 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001677 Negative = false;
1678 OffsetRegShifted = false;
1679 OffsetIsReg = false;
1680 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001681 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001682 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001683 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001684 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001685 else if (NextTok.is(AsmToken::Minus)) {
1686 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001687 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001688 }
1689 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001690 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001691 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001692 SMLoc CurLoc = OffsetRegTok.getLoc();
1693 OffsetRegNum = TryParseRegister();
1694 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001695 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001696 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001697 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001698 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001699
Bill Wendling12f40e92010-11-06 10:51:53 +00001700 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001701 if (OffsetRegNum != -1) {
1702 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001703 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001704 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001705 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001706
Sean Callanan18b83232010-01-19 21:44:56 +00001707 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001708 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001709 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001710 OffsetRegShifted = true;
1711 }
1712 }
1713 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1714 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001715 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001716 if (HashTok.isNot(AsmToken::Hash))
1717 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001718
Sean Callananb9a25b72010-01-19 20:27:46 +00001719 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001720
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001721 if (getParser().ParseExpression(Offset))
1722 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001723 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001724 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001725 return false;
1726}
1727
1728/// ParseShift as one of these two:
1729/// ( lsl | lsr | asr | ror ) , # shift_amount
1730/// rrx
1731/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001732bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1733 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001734 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001735 if (Tok.isNot(AsmToken::Identifier))
1736 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001737 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001738 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001739 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001740 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001741 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001742 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001743 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001744 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001745 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001746 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001747 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001748 else
1749 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001750 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001751
1752 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001753 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001754 return false;
1755
1756 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001757 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001758 if (HashTok.isNot(AsmToken::Hash))
1759 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001760 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001761
1762 if (getParser().ParseExpression(ShiftAmount))
1763 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001764
1765 return false;
1766}
1767
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001768/// Parse a arm instruction operand. For now this parses the operand regardless
1769/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001770bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001771 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001772 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001773
1774 // Check if the current operand has a custom associated parser, if so, try to
1775 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001776 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1777 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001778 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001779 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1780 // there was a match, but an error occurred, in which case, just return that
1781 // the operand parsing failed.
1782 if (ResTy == MatchOperand_ParseFail)
1783 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001784
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001785 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001786 default:
1787 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001788 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001789 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001790 if (!TryParseRegisterWithWriteBack(Operands))
1791 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001792 int Res = TryParseShiftRegister(Operands);
1793 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001794 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001795 else if (Res == -1) // irrecoverable error
1796 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001797
1798 // Fall though for the Identifier case that is not a register or a
1799 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00001800 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00001801 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1802 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001803 // This was not a register so parse other operands that start with an
1804 // identifier (like labels) as expressions and create them as immediates.
1805 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001806 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001807 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001808 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001809 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001810 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1811 return false;
1812 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001813 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001814 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001815 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001816 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001817 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001818 // #42 -> immediate.
1819 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001820 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001821 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001822 const MCExpr *ImmVal;
1823 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001824 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001825 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001826 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1827 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001828 case AsmToken::Colon: {
1829 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001830 // FIXME: Check it's an expression prefix,
1831 // e.g. (FOO - :lower16:BAR) isn't legal.
1832 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001833 if (ParsePrefix(RefKind))
1834 return true;
1835
Evan Cheng75972122011-01-13 07:58:56 +00001836 const MCExpr *SubExprVal;
1837 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001838 return true;
1839
Evan Cheng75972122011-01-13 07:58:56 +00001840 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1841 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001842 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001843 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001844 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001845 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001846 }
1847}
1848
Evan Cheng75972122011-01-13 07:58:56 +00001849// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1850// :lower16: and :upper16:.
1851bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1852 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001853
1854 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001855 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001856 Parser.Lex(); // Eat ':'
1857
1858 if (getLexer().isNot(AsmToken::Identifier)) {
1859 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1860 return true;
1861 }
1862
1863 StringRef IDVal = Parser.getTok().getIdentifier();
1864 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001865 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001866 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001867 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001868 } else {
1869 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1870 return true;
1871 }
1872 Parser.Lex();
1873
1874 if (getLexer().isNot(AsmToken::Colon)) {
1875 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1876 return true;
1877 }
1878 Parser.Lex(); // Eat the last ':'
1879 return false;
1880}
1881
1882const MCExpr *
1883ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1884 MCSymbolRefExpr::VariantKind Variant) {
1885 // Recurse over the given expression, rebuilding it to apply the given variant
1886 // to the leftmost symbol.
1887 if (Variant == MCSymbolRefExpr::VK_None)
1888 return E;
1889
1890 switch (E->getKind()) {
1891 case MCExpr::Target:
1892 llvm_unreachable("Can't handle target expr yet");
1893 case MCExpr::Constant:
1894 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1895
1896 case MCExpr::SymbolRef: {
1897 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1898
1899 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1900 return 0;
1901
1902 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1903 }
1904
1905 case MCExpr::Unary:
1906 llvm_unreachable("Can't handle unary expressions yet");
1907
1908 case MCExpr::Binary: {
1909 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1910 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1911 const MCExpr *RHS = BE->getRHS();
1912 if (!LHS)
1913 return 0;
1914
1915 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1916 }
1917 }
1918
1919 assert(0 && "Invalid expression kind!");
1920 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001921}
1922
Daniel Dunbar352e1482011-01-11 15:59:50 +00001923/// \brief Given a mnemonic, split out possible predication code and carry
1924/// setting letters to form a canonical mnemonic and flags.
1925//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001926// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001927static StringRef SplitMnemonic(StringRef Mnemonic,
1928 unsigned &PredicationCode,
1929 bool &CarrySetting,
1930 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001931 PredicationCode = ARMCC::AL;
1932 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001933 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001934
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001935 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001936 //
1937 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001938 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1939 Mnemonic == "movs" ||
1940 Mnemonic == "svc" ||
1941 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1942 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1943 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1944 Mnemonic == "vclt" ||
1945 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1946 Mnemonic == "vcle" ||
1947 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1948 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbachd1f0bbe2011-06-27 20:59:10 +00001949 Mnemonic == "vqdmlal" || Mnemonic == "bics"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001950 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001951
Jim Grosbach3f00e312011-07-11 17:09:57 +00001952 // First, split out any predication code. Ignore mnemonics we know aren't
1953 // predicated but do have a carry-set and so weren't caught above.
1954 if (Mnemonic != "adcs") {
1955 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1956 .Case("eq", ARMCC::EQ)
1957 .Case("ne", ARMCC::NE)
1958 .Case("hs", ARMCC::HS)
1959 .Case("cs", ARMCC::HS)
1960 .Case("lo", ARMCC::LO)
1961 .Case("cc", ARMCC::LO)
1962 .Case("mi", ARMCC::MI)
1963 .Case("pl", ARMCC::PL)
1964 .Case("vs", ARMCC::VS)
1965 .Case("vc", ARMCC::VC)
1966 .Case("hi", ARMCC::HI)
1967 .Case("ls", ARMCC::LS)
1968 .Case("ge", ARMCC::GE)
1969 .Case("lt", ARMCC::LT)
1970 .Case("gt", ARMCC::GT)
1971 .Case("le", ARMCC::LE)
1972 .Case("al", ARMCC::AL)
1973 .Default(~0U);
1974 if (CC != ~0U) {
1975 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
1976 PredicationCode = CC;
1977 }
Bill Wendling52925b62010-10-29 23:50:21 +00001978 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001979
Daniel Dunbar352e1482011-01-11 15:59:50 +00001980 // Next, determine if we have a carry setting bit. We explicitly ignore all
1981 // the instructions we know end in 's'.
1982 if (Mnemonic.endswith("s") &&
1983 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1984 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1985 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1986 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1987 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1988 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1989 CarrySetting = true;
1990 }
1991
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001992 // The "cps" instruction can have a interrupt mode operand which is glued into
1993 // the mnemonic. Check if this is the case, split it and parse the imod op
1994 if (Mnemonic.startswith("cps")) {
1995 // Split out any imod code.
1996 unsigned IMod =
1997 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1998 .Case("ie", ARM_PROC::IE)
1999 .Case("id", ARM_PROC::ID)
2000 .Default(~0U);
2001 if (IMod != ~0U) {
2002 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2003 ProcessorIMod = IMod;
2004 }
2005 }
2006
Daniel Dunbar352e1482011-01-11 15:59:50 +00002007 return Mnemonic;
2008}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002009
2010/// \brief Given a canonical mnemonic, determine if the instruction ever allows
2011/// inclusion of carry set or predication code operands.
2012//
2013// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00002014void ARMAsmParser::
2015GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2016 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002017 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2018 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2019 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2020 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002021 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002022 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2023 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002024 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00002025 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002026 CanAcceptCarrySet = true;
2027 } else {
2028 CanAcceptCarrySet = false;
2029 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002030
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002031 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2032 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2033 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2034 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00002035 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002036 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002037 CanAcceptPredicationCode = false;
2038 } else {
2039 CanAcceptPredicationCode = true;
2040 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002041
Evan Chengebdeeab2011-07-08 01:53:10 +00002042 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002043 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002044 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002045 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002046}
2047
2048/// Parse an arm instruction mnemonic followed by its operands.
2049bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2050 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2051 // Create the leading tokens for the mnemonic, split by '.' characters.
2052 size_t Start = 0, Next = Name.find('.');
2053 StringRef Head = Name.slice(Start, Next);
2054
Daniel Dunbar352e1482011-01-11 15:59:50 +00002055 // Split out the predication code and carry setting flag from the mnemonic.
2056 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002057 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002058 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002059 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
2060 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002061
Chris Lattner3a697562010-10-28 17:20:03 +00002062 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00002063
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002064 // Next, add the CCOut and ConditionCode operands, if needed.
2065 //
2066 // For mnemonics which can ever incorporate a carry setting bit or predication
2067 // code, our matching model involves us always generating CCOut and
2068 // ConditionCode operands to match the mnemonic "as written" and then we let
2069 // the matcher deal with finding the right instruction or generating an
2070 // appropriate error.
2071 bool CanAcceptCarrySet, CanAcceptPredicationCode;
2072 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
2073
2074 // Add the carry setting operand, if necessary.
2075 //
2076 // FIXME: It would be awesome if we could somehow invent a location such that
2077 // match errors on this operand would print a nice diagnostic about how the
2078 // 's' character in the mnemonic resulted in a CCOut operand.
2079 if (CanAcceptCarrySet) {
2080 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2081 NameLoc));
2082 } else {
2083 // This mnemonic can't ever accept a carry set, but the user wrote one (or
2084 // misspelled another mnemonic).
2085
2086 // FIXME: Issue a nice error.
2087 }
2088
2089 // Add the predication code operand, if necessary.
2090 if (CanAcceptPredicationCode) {
2091 Operands.push_back(ARMOperand::CreateCondCode(
2092 ARMCC::CondCodes(PredicationCode), NameLoc));
2093 } else {
2094 // This mnemonic can't ever accept a predication code, but the user wrote
2095 // one (or misspelled another mnemonic).
2096
2097 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002098 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002099
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002100 // Add the processor imod operand, if necessary.
2101 if (ProcessorIMod) {
2102 Operands.push_back(ARMOperand::CreateImm(
2103 MCConstantExpr::Create(ProcessorIMod, getContext()),
2104 NameLoc, NameLoc));
2105 } else {
2106 // This mnemonic can't ever accept a imod, but the user wrote
2107 // one (or misspelled another mnemonic).
2108
2109 // FIXME: Issue a nice error.
2110 }
2111
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002112 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002113 while (Next != StringRef::npos) {
2114 Start = Next;
2115 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002116 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002117
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002118 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002119 }
2120
2121 // Read the remaining operands.
2122 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002123 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002124 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002125 Parser.EatToEndOfStatement();
2126 return true;
2127 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002128
2129 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002130 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002131
2132 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002133 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002134 Parser.EatToEndOfStatement();
2135 return true;
2136 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002137 }
2138 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002139
Chris Lattnercbf8a982010-09-11 16:18:25 +00002140 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2141 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002142 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002143 }
Bill Wendling146018f2010-11-06 21:42:12 +00002144
Chris Lattner34e53142010-09-08 05:10:46 +00002145 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00002146 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002147}
2148
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002149bool ARMAsmParser::
2150MatchAndEmitInstruction(SMLoc IDLoc,
2151 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2152 MCStreamer &Out) {
2153 MCInst Inst;
2154 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002155 MatchResultTy MatchResult, MatchResult2;
2156 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2157 if (MatchResult != Match_Success) {
2158 // If we get a Match_InvalidOperand it might be some arithmetic instruction
2159 // that does not update the condition codes. So try adding a CCOut operand
2160 // with a value of reg0.
2161 if (MatchResult == Match_InvalidOperand) {
2162 Operands.insert(Operands.begin() + 1,
2163 ARMOperand::CreateCCOut(0,
2164 ((ARMOperand*)Operands[0])->getStartLoc()));
2165 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2166 if (MatchResult2 == Match_Success)
2167 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002168 else {
2169 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002170 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002171 delete CCOut;
2172 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002173 }
2174 // If we get a Match_MnemonicFail it might be some arithmetic instruction
2175 // that updates the condition codes if it ends in 's'. So see if the
2176 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
2177 // operand with a value of CPSR.
Evan Chengeb0caa12011-07-08 22:49:55 +00002178 else if (MatchResult == Match_MnemonicFail) {
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002179 // Get the instruction mnemonic, which is the first token.
2180 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
2181 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
2182 // removed the 's' from the mnemonic for matching.
2183 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
2184 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002185 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2186 Operands.erase(Operands.begin());
2187 delete OldMnemonic;
2188 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002189 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
2190 Operands.insert(Operands.begin() + 1,
2191 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2192 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2193 if (MatchResult2 == Match_Success)
2194 MatchResult = Match_Success;
2195 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002196 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2197 Operands.erase(Operands.begin());
2198 delete OldMnemonic;
2199 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002200 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002201 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2202 Operands.erase(Operands.begin() + 1);
2203 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002204 }
2205 }
2206 }
2207 }
2208 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002209 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002210 Out.EmitInstruction(Inst);
2211 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002212 case Match_MissingFeature:
2213 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2214 return true;
2215 case Match_InvalidOperand: {
2216 SMLoc ErrorLoc = IDLoc;
2217 if (ErrorInfo != ~0U) {
2218 if (ErrorInfo >= Operands.size())
2219 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002220
Chris Lattnere73d4f82010-10-28 21:41:58 +00002221 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2222 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2223 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002224
Chris Lattnere73d4f82010-10-28 21:41:58 +00002225 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002226 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002227 case Match_MnemonicFail:
2228 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002229 case Match_ConversionFail:
2230 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002231 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002232
Eric Christopherc223e2b2010-10-29 09:26:59 +00002233 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002234 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002235}
2236
Kevin Enderby515d5092009-10-15 20:48:48 +00002237/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002238bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2239 StringRef IDVal = DirectiveID.getIdentifier();
2240 if (IDVal == ".word")
2241 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002242 else if (IDVal == ".thumb")
2243 return ParseDirectiveThumb(DirectiveID.getLoc());
2244 else if (IDVal == ".thumb_func")
2245 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2246 else if (IDVal == ".code")
2247 return ParseDirectiveCode(DirectiveID.getLoc());
2248 else if (IDVal == ".syntax")
2249 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002250 return true;
2251}
2252
2253/// ParseDirectiveWord
2254/// ::= .word [ expression (, expression)* ]
2255bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2256 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2257 for (;;) {
2258 const MCExpr *Value;
2259 if (getParser().ParseExpression(Value))
2260 return true;
2261
Chris Lattneraaec2052010-01-19 19:46:13 +00002262 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002263
2264 if (getLexer().is(AsmToken::EndOfStatement))
2265 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002266
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002267 // FIXME: Improve diagnostic.
2268 if (getLexer().isNot(AsmToken::Comma))
2269 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002270 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002271 }
2272 }
2273
Sean Callananb9a25b72010-01-19 20:27:46 +00002274 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002275 return false;
2276}
2277
Kevin Enderby515d5092009-10-15 20:48:48 +00002278/// ParseDirectiveThumb
2279/// ::= .thumb
2280bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2281 if (getLexer().isNot(AsmToken::EndOfStatement))
2282 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002283 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002284
2285 // TODO: set thumb mode
2286 // TODO: tell the MC streamer the mode
2287 // getParser().getStreamer().Emit???();
2288 return false;
2289}
2290
2291/// ParseDirectiveThumbFunc
2292/// ::= .thumbfunc symbol_name
2293bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002294 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2295 bool isMachO = MAI.hasSubsectionsViaSymbols();
2296 StringRef Name;
2297
2298 // Darwin asm has function name after .thumb_func direction
2299 // ELF doesn't
2300 if (isMachO) {
2301 const AsmToken &Tok = Parser.getTok();
2302 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2303 return Error(L, "unexpected token in .thumb_func directive");
2304 Name = Tok.getString();
2305 Parser.Lex(); // Consume the identifier token.
2306 }
2307
Kevin Enderby515d5092009-10-15 20:48:48 +00002308 if (getLexer().isNot(AsmToken::EndOfStatement))
2309 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002310 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002311
Rafael Espindola64695402011-05-16 16:17:21 +00002312 // FIXME: assuming function name will be the line following .thumb_func
2313 if (!isMachO) {
2314 Name = Parser.getTok().getString();
2315 }
2316
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002317 // Mark symbol as a thumb symbol.
2318 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2319 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002320 return false;
2321}
2322
2323/// ParseDirectiveSyntax
2324/// ::= .syntax unified | divided
2325bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002326 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002327 if (Tok.isNot(AsmToken::Identifier))
2328 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002329 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002330 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002331 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002332 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002333 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002334 else
2335 return Error(L, "unrecognized syntax mode in .syntax directive");
2336
2337 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002338 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002339 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002340
2341 // TODO tell the MC streamer the mode
2342 // getParser().getStreamer().Emit???();
2343 return false;
2344}
2345
2346/// ParseDirectiveCode
2347/// ::= .code 16 | 32
2348bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002349 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002350 if (Tok.isNot(AsmToken::Integer))
2351 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002352 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002353 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002354 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002355 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002356 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002357 else
2358 return Error(L, "invalid operand to .code directive");
2359
2360 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002361 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002362 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002363
Evan Cheng32869202011-07-08 22:36:29 +00002364 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002365 if (!isThumb())
2366 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002367 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002368 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002369 if (isThumb())
2370 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002371 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002372 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002373
Kevin Enderby515d5092009-10-15 20:48:48 +00002374 return false;
2375}
2376
Sean Callanan90b70972010-04-07 20:29:34 +00002377extern "C" void LLVMInitializeARMAsmLexer();
2378
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002379/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002380extern "C" void LLVMInitializeARMAsmParser() {
2381 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2382 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002383 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002384}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002385
Chris Lattner0692ee62010-09-06 19:11:01 +00002386#define GET_REGISTER_MATCHER
2387#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002388#include "ARMGenAsmMatcher.inc"