blob: 129af206e1d9e274fb694548a010cb6e0721c8ad [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"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000018#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000019#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000022#include "llvm/Target/TargetRegistry.h"
23#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000027#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000028#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000030using namespace llvm;
31
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +000032/// Shift types used for register controlled shifts in ARM memory addressing.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000033enum ShiftType {
34 Lsl,
35 Lsr,
36 Asr,
37 Ror,
38 Rrx
39};
40
Chris Lattner3a697562010-10-28 17:20:03 +000041namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000042
43class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000044
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045class ARMAsmParser : public TargetAsmParser {
46 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000047 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000049 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000050 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
51
52 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000053 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
54
Chris Lattnere5658fa2010-10-30 04:09:10 +000055 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000056 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000057 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
58 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
59 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000060 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000061 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000062 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
63 MCSymbolRefExpr::VariantKind Variant);
64
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000065
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 bool ParseMemoryOffsetReg(bool &Negative,
67 bool &OffsetRegShifted,
68 enum ShiftType &ShiftType,
69 const MCExpr *&ShiftAmount,
70 const MCExpr *&Offset,
71 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000072 int &OffsetRegNum,
73 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000074 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000075 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000076 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000078 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveSyntax(SMLoc L);
80
Chris Lattner7036f8b2010-09-29 01:42:58 +000081 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000082 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000083 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000084 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
85 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000086
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000087 /// @name Auto-generated Match Functions
88 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000089
Chris Lattner0692ee62010-09-06 19:11:01 +000090#define GET_ASSEMBLER_HEADER
91#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000092
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000093 /// }
94
Jim Grosbachf922c472011-02-12 01:34:40 +000095 OperandMatchResultTy tryParseCoprocNumOperand(
96 SmallVectorImpl<MCParsedAsmOperand*>&);
97 OperandMatchResultTy tryParseCoprocRegOperand(
98 SmallVectorImpl<MCParsedAsmOperand*>&);
99 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000100 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000101 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000102 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000103 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000104 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbachf922c472011-02-12 01:34:40 +0000105
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000106public:
Daniel Dunbard73ada72010-07-19 00:33:49 +0000107 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +0000108 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
109 // Initialize the set of available features.
110 setAvailableFeatures(ComputeAvailableFeatures(
111 &TM.getSubtarget<ARMSubtarget>()));
112 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000113
Benjamin Kramer38e59892010-07-14 22:38:02 +0000114 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000115 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000116 virtual bool ParseDirective(AsmToken DirectiveID);
117};
Jim Grosbach16c74252010-10-29 14:46:02 +0000118} // end anonymous namespace
119
Chris Lattner3a697562010-10-28 17:20:03 +0000120namespace {
121
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000122/// ARMOperand - Instances of this class represent a parsed ARM machine
123/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000124class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000125 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000126 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000127 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000128 CoprocNum,
129 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000130 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000131 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000132 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000133 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000134 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000135 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000136 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000137 DPRRegisterList,
138 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000139 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000140 } Kind;
141
Sean Callanan76264762010-04-02 22:27:05 +0000142 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000143 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000144
145 union {
146 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000147 ARMCC::CondCodes Val;
148 } CC;
149
150 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000151 ARM_MB::MemBOpt Val;
152 } MBOpt;
153
154 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000155 unsigned Val;
156 } Cop;
157
158 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000159 ARM_PROC::IFlags Val;
160 } IFlags;
161
162 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000163 unsigned Val;
164 } MMask;
165
166 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000167 const char *Data;
168 unsigned Length;
169 } Tok;
170
171 struct {
172 unsigned RegNum;
173 } Reg;
174
Bill Wendling8155e5b2010-11-06 22:19:43 +0000175 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000176 const MCExpr *Val;
177 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000178
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000179 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000180 struct {
181 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000182 union {
183 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
184 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
185 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000186 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
187 enum ShiftType ShiftType; // used when OffsetRegShifted is true
188 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000189 unsigned Preindexed : 1;
190 unsigned Postindexed : 1;
191 unsigned OffsetIsReg : 1;
192 unsigned Negative : 1; // only used when OffsetIsReg is true
193 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000194 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000195 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000196
Bill Wendling146018f2010-11-06 21:42:12 +0000197 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
198public:
Sean Callanan76264762010-04-02 22:27:05 +0000199 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
200 Kind = o.Kind;
201 StartLoc = o.StartLoc;
202 EndLoc = o.EndLoc;
203 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000204 case CondCode:
205 CC = o.CC;
206 break;
Sean Callanan76264762010-04-02 22:27:05 +0000207 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000208 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000209 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000210 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000211 case Register:
212 Reg = o.Reg;
213 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000214 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000215 case DPRRegisterList:
216 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000217 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000218 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000219 case CoprocNum:
220 case CoprocReg:
221 Cop = o.Cop;
222 break;
Sean Callanan76264762010-04-02 22:27:05 +0000223 case Immediate:
224 Imm = o.Imm;
225 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000226 case MemBarrierOpt:
227 MBOpt = o.MBOpt;
228 break;
Sean Callanan76264762010-04-02 22:27:05 +0000229 case Memory:
230 Mem = o.Mem;
231 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000232 case MSRMask:
233 MMask = o.MMask;
234 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000235 case ProcIFlags:
236 IFlags = o.IFlags;
Sean Callanan76264762010-04-02 22:27:05 +0000237 }
238 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000239
Sean Callanan76264762010-04-02 22:27:05 +0000240 /// getStartLoc - Get the location of the first token of this operand.
241 SMLoc getStartLoc() const { return StartLoc; }
242 /// getEndLoc - Get the location of the last token of this operand.
243 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000244
Daniel Dunbar8462b302010-08-11 06:36:53 +0000245 ARMCC::CondCodes getCondCode() const {
246 assert(Kind == CondCode && "Invalid access!");
247 return CC.Val;
248 }
249
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000250 unsigned getCoproc() const {
251 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
252 return Cop.Val;
253 }
254
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000255 StringRef getToken() const {
256 assert(Kind == Token && "Invalid access!");
257 return StringRef(Tok.Data, Tok.Length);
258 }
259
260 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000261 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000262 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000263 }
264
Bill Wendling5fa22a12010-11-09 23:28:44 +0000265 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000266 assert((Kind == RegisterList || Kind == DPRRegisterList ||
267 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000268 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000269 }
270
Kevin Enderbycfe07242009-10-13 22:19:02 +0000271 const MCExpr *getImm() const {
272 assert(Kind == Immediate && "Invalid access!");
273 return Imm.Val;
274 }
275
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000276 ARM_MB::MemBOpt getMemBarrierOpt() const {
277 assert(Kind == MemBarrierOpt && "Invalid access!");
278 return MBOpt.Val;
279 }
280
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000281 ARM_PROC::IFlags getProcIFlags() const {
282 assert(Kind == ProcIFlags && "Invalid access!");
283 return IFlags.Val;
284 }
285
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000286 unsigned getMSRMask() const {
287 assert(Kind == MSRMask && "Invalid access!");
288 return MMask.Val;
289 }
290
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000291 /// @name Memory Operand Accessors
292 /// @{
293
294 unsigned getMemBaseRegNum() const {
295 return Mem.BaseRegNum;
296 }
297 unsigned getMemOffsetRegNum() const {
298 assert(Mem.OffsetIsReg && "Invalid access!");
299 return Mem.Offset.RegNum;
300 }
301 const MCExpr *getMemOffset() const {
302 assert(!Mem.OffsetIsReg && "Invalid access!");
303 return Mem.Offset.Value;
304 }
305 unsigned getMemOffsetRegShifted() const {
306 assert(Mem.OffsetIsReg && "Invalid access!");
307 return Mem.OffsetRegShifted;
308 }
309 const MCExpr *getMemShiftAmount() const {
310 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
311 return Mem.ShiftAmount;
312 }
313 enum ShiftType getMemShiftType() const {
314 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
315 return Mem.ShiftType;
316 }
317 bool getMemPreindexed() const { return Mem.Preindexed; }
318 bool getMemPostindexed() const { return Mem.Postindexed; }
319 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
320 bool getMemNegative() const { return Mem.Negative; }
321 bool getMemWriteback() const { return Mem.Writeback; }
322
323 /// @}
324
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000325 bool isCoprocNum() const { return Kind == CoprocNum; }
326 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000327 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000328 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000329 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000330 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000331 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000332 bool isDPRRegList() const { return Kind == DPRRegisterList; }
333 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000334 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000335 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000336 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000337 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000338 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
339 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000340 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000341
Daniel Dunbar4b462672011-01-18 05:55:27 +0000342 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000343 if (!CE) return false;
344
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000345 // The offset must be a multiple of 4 in the range 0-1020.
346 int64_t Value = CE->getValue();
347 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
348 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000349 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000350 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000351 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000352 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000353 }
354 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000355 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000356 return false;
357
Daniel Dunbar4b462672011-01-18 05:55:27 +0000358 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000359 if (!CE) return false;
360
361 // The offset must be a multiple of 4 in the range 0-124.
362 uint64_t Value = CE->getValue();
363 return ((Value & 0x3) == 0 && Value <= 124);
364 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000365 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000366 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000367
368 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000369 // Add as immediates when possible. Null MCExpr = 0.
370 if (Expr == 0)
371 Inst.addOperand(MCOperand::CreateImm(0));
372 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000373 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
374 else
375 Inst.addOperand(MCOperand::CreateExpr(Expr));
376 }
377
Daniel Dunbar8462b302010-08-11 06:36:53 +0000378 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000379 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000380 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000381 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
382 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000383 }
384
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000385 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
386 assert(N == 1 && "Invalid number of operands!");
387 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
388 }
389
390 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
391 assert(N == 1 && "Invalid number of operands!");
392 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
393 }
394
Jim Grosbachd67641b2010-12-06 18:21:12 +0000395 void addCCOutOperands(MCInst &Inst, unsigned N) const {
396 assert(N == 1 && "Invalid number of operands!");
397 Inst.addOperand(MCOperand::CreateReg(getReg()));
398 }
399
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000400 void addRegOperands(MCInst &Inst, unsigned N) const {
401 assert(N == 1 && "Invalid number of operands!");
402 Inst.addOperand(MCOperand::CreateReg(getReg()));
403 }
404
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000405 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000406 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000407 const SmallVectorImpl<unsigned> &RegList = getRegList();
408 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000409 I = RegList.begin(), E = RegList.end(); I != E; ++I)
410 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000411 }
412
Bill Wendling0f630752010-11-17 04:32:08 +0000413 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
414 addRegListOperands(Inst, N);
415 }
416
417 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
418 addRegListOperands(Inst, N);
419 }
420
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000421 void addImmOperands(MCInst &Inst, unsigned N) const {
422 assert(N == 1 && "Invalid number of operands!");
423 addExpr(Inst, getImm());
424 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000425
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000426 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
427 assert(N == 1 && "Invalid number of operands!");
428 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
429 }
430
Chris Lattner14b93852010-10-29 00:27:31 +0000431 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
432 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000433
Daniel Dunbar4b462672011-01-18 05:55:27 +0000434 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
435 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000436
Jim Grosbach80eb2332010-10-29 17:41:25 +0000437 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
438 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000439 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000440 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000441
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000442 // The MCInst offset operand doesn't include the low two bits (like
443 // the instruction encoding).
444 int64_t Offset = CE->getValue() / 4;
445 if (Offset >= 0)
446 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
447 Offset)));
448 else
449 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
450 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000451 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000452
Bill Wendlingf4caf692010-12-14 03:36:38 +0000453 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
454 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000455 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
456 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000457 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000458
Bill Wendlingf4caf692010-12-14 03:36:38 +0000459 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
460 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000461 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
462 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000463 assert(CE && "Non-constant mode offset operand!");
464 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000465 }
466
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000467 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
468 assert(N == 1 && "Invalid number of operands!");
469 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
470 }
471
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000472 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
473 assert(N == 1 && "Invalid number of operands!");
474 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
475 }
476
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000477 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000478
Chris Lattner3a697562010-10-28 17:20:03 +0000479 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
480 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000481 Op->CC.Val = CC;
482 Op->StartLoc = S;
483 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000484 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000485 }
486
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000487 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
488 ARMOperand *Op = new ARMOperand(CoprocNum);
489 Op->Cop.Val = CopVal;
490 Op->StartLoc = S;
491 Op->EndLoc = S;
492 return Op;
493 }
494
495 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
496 ARMOperand *Op = new ARMOperand(CoprocReg);
497 Op->Cop.Val = CopVal;
498 Op->StartLoc = S;
499 Op->EndLoc = S;
500 return Op;
501 }
502
Jim Grosbachd67641b2010-12-06 18:21:12 +0000503 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
504 ARMOperand *Op = new ARMOperand(CCOut);
505 Op->Reg.RegNum = RegNum;
506 Op->StartLoc = S;
507 Op->EndLoc = S;
508 return Op;
509 }
510
Chris Lattner3a697562010-10-28 17:20:03 +0000511 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
512 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000513 Op->Tok.Data = Str.data();
514 Op->Tok.Length = Str.size();
515 Op->StartLoc = S;
516 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000517 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000518 }
519
Bill Wendling50d0f582010-11-18 23:43:05 +0000520 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000521 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000522 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000523 Op->StartLoc = S;
524 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000525 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000526 }
527
Bill Wendling7729e062010-11-09 22:44:22 +0000528 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000529 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000530 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000531 KindTy Kind = RegisterList;
532
533 if (ARM::DPRRegClass.contains(Regs.front().first))
534 Kind = DPRRegisterList;
535 else if (ARM::SPRRegClass.contains(Regs.front().first))
536 Kind = SPRRegisterList;
537
538 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000539 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000540 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000541 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000542 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000543 Op->StartLoc = StartLoc;
544 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000545 return Op;
546 }
547
Chris Lattner3a697562010-10-28 17:20:03 +0000548 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
549 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000550 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000551 Op->StartLoc = S;
552 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000553 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000554 }
555
Chris Lattner3a697562010-10-28 17:20:03 +0000556 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000557 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000558 bool OffsetRegShifted, enum ShiftType ShiftType,
559 const MCExpr *ShiftAmount, bool Preindexed,
560 bool Postindexed, bool Negative, bool Writeback,
561 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000562 assert((OffsetRegNum == -1 || OffsetIsReg) &&
563 "OffsetRegNum must imply OffsetIsReg!");
564 assert((!OffsetRegShifted || OffsetIsReg) &&
565 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000566 assert((Offset || OffsetIsReg) &&
567 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000568 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
569 "Cannot have shift amount without shifted register offset!");
570 assert((!Offset || !OffsetIsReg) &&
571 "Cannot have expression offset and register offset!");
572
Chris Lattner3a697562010-10-28 17:20:03 +0000573 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000574 Op->Mem.BaseRegNum = BaseRegNum;
575 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000576 if (OffsetIsReg)
577 Op->Mem.Offset.RegNum = OffsetRegNum;
578 else
579 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000580 Op->Mem.OffsetRegShifted = OffsetRegShifted;
581 Op->Mem.ShiftType = ShiftType;
582 Op->Mem.ShiftAmount = ShiftAmount;
583 Op->Mem.Preindexed = Preindexed;
584 Op->Mem.Postindexed = Postindexed;
585 Op->Mem.Negative = Negative;
586 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000587
Sean Callanan76264762010-04-02 22:27:05 +0000588 Op->StartLoc = S;
589 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000590 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000591 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000592
593 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
594 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
595 Op->MBOpt.Val = Opt;
596 Op->StartLoc = S;
597 Op->EndLoc = S;
598 return Op;
599 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000600
601 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
602 ARMOperand *Op = new ARMOperand(ProcIFlags);
603 Op->IFlags.Val = IFlags;
604 Op->StartLoc = S;
605 Op->EndLoc = S;
606 return Op;
607 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000608
609 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
610 ARMOperand *Op = new ARMOperand(MSRMask);
611 Op->MMask.Val = MMask;
612 Op->StartLoc = S;
613 Op->EndLoc = S;
614 return Op;
615 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000616};
617
618} // end anonymous namespace.
619
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000620void ARMOperand::dump(raw_ostream &OS) const {
621 switch (Kind) {
622 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000623 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000624 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000625 case CCOut:
626 OS << "<ccout " << getReg() << ">";
627 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000628 case CoprocNum:
629 OS << "<coprocessor number: " << getCoproc() << ">";
630 break;
631 case CoprocReg:
632 OS << "<coprocessor register: " << getCoproc() << ">";
633 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000634 case MSRMask:
635 OS << "<mask: " << getMSRMask() << ">";
636 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000637 case Immediate:
638 getImm()->print(OS);
639 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000640 case MemBarrierOpt:
641 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
642 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000643 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000644 OS << "<memory "
645 << "base:" << getMemBaseRegNum();
646 if (getMemOffsetIsReg()) {
647 OS << " offset:<register " << getMemOffsetRegNum();
648 if (getMemOffsetRegShifted()) {
649 OS << " offset-shift-type:" << getMemShiftType();
650 OS << " offset-shift-amount:" << *getMemShiftAmount();
651 }
652 } else {
653 OS << " offset:" << *getMemOffset();
654 }
655 if (getMemOffsetIsReg())
656 OS << " (offset-is-reg)";
657 if (getMemPreindexed())
658 OS << " (pre-indexed)";
659 if (getMemPostindexed())
660 OS << " (post-indexed)";
661 if (getMemNegative())
662 OS << " (negative)";
663 if (getMemWriteback())
664 OS << " (writeback)";
665 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000666 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000667 case ProcIFlags: {
668 OS << "<ARM_PROC::";
669 unsigned IFlags = getProcIFlags();
670 for (int i=2; i >= 0; --i)
671 if (IFlags & (1 << i))
672 OS << ARM_PROC::IFlagsToString(1 << i);
673 OS << ">";
674 break;
675 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000676 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000677 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000678 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000679 case RegisterList:
680 case DPRRegisterList:
681 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000682 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000683
Bill Wendling5fa22a12010-11-09 23:28:44 +0000684 const SmallVectorImpl<unsigned> &RegList = getRegList();
685 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000686 I = RegList.begin(), E = RegList.end(); I != E; ) {
687 OS << *I;
688 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000689 }
690
691 OS << ">";
692 break;
693 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000694 case Token:
695 OS << "'" << getToken() << "'";
696 break;
697 }
698}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000699
700/// @name Auto-generated Match Functions
701/// {
702
703static unsigned MatchRegisterName(StringRef Name);
704
705/// }
706
Bob Wilson69df7232011-02-03 21:46:10 +0000707bool ARMAsmParser::ParseRegister(unsigned &RegNo,
708 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000709 RegNo = TryParseRegister();
710
711 return (RegNo == (unsigned)-1);
712}
713
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000714/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000715/// and if it is a register name the token is eaten and the register number is
716/// returned. Otherwise return -1.
717///
718int ARMAsmParser::TryParseRegister() {
719 const AsmToken &Tok = Parser.getTok();
720 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000721
Chris Lattnere5658fa2010-10-30 04:09:10 +0000722 // FIXME: Validate register for the current architecture; we have to do
723 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000724 std::string upperCase = Tok.getString().str();
725 std::string lowerCase = LowercaseString(upperCase);
726 unsigned RegNum = MatchRegisterName(lowerCase);
727 if (!RegNum) {
728 RegNum = StringSwitch<unsigned>(lowerCase)
729 .Case("r13", ARM::SP)
730 .Case("r14", ARM::LR)
731 .Case("r15", ARM::PC)
732 .Case("ip", ARM::R12)
733 .Default(0);
734 }
735 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000736
Chris Lattnere5658fa2010-10-30 04:09:10 +0000737 Parser.Lex(); // Eat identifier token.
738 return RegNum;
739}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000740
Bill Wendling50d0f582010-11-18 23:43:05 +0000741/// Try to parse a register name. The token must be an Identifier when called.
742/// If it's a register, an AsmOperand is created. Another AsmOperand is created
743/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000744///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000745/// TODO this is likely to change to allow different register types and or to
746/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000747bool ARMAsmParser::
748TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000749 SMLoc S = Parser.getTok().getLoc();
750 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000751 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000752 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000753
Bill Wendling50d0f582010-11-18 23:43:05 +0000754 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000755
Chris Lattnere5658fa2010-10-30 04:09:10 +0000756 const AsmToken &ExclaimTok = Parser.getTok();
757 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000758 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
759 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000760 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000761 }
762
Bill Wendling50d0f582010-11-18 23:43:05 +0000763 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000764}
765
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000766/// MatchCoprocessorOperandName - Try to parse an coprocessor related
767/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
768/// "c5", ...
769static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000770 // Use the same layout as the tablegen'erated register name matcher. Ugly,
771 // but efficient.
772 switch (Name.size()) {
773 default: break;
774 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000775 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000776 return -1;
777 switch (Name[1]) {
778 default: return -1;
779 case '0': return 0;
780 case '1': return 1;
781 case '2': return 2;
782 case '3': return 3;
783 case '4': return 4;
784 case '5': return 5;
785 case '6': return 6;
786 case '7': return 7;
787 case '8': return 8;
788 case '9': return 9;
789 }
790 break;
791 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000792 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000793 return -1;
794 switch (Name[2]) {
795 default: return -1;
796 case '0': return 10;
797 case '1': return 11;
798 case '2': return 12;
799 case '3': return 13;
800 case '4': return 14;
801 case '5': return 15;
802 }
803 break;
804 }
805
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000806 return -1;
807}
808
Jim Grosbachf922c472011-02-12 01:34:40 +0000809/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000810/// token must be an Identifier when called, and if it is a coprocessor
811/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000812ARMAsmParser::OperandMatchResultTy ARMAsmParser::
813tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000814 SMLoc S = Parser.getTok().getLoc();
815 const AsmToken &Tok = Parser.getTok();
816 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
817
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000818 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000819 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +0000820 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000821
822 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000823 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000824 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000825}
826
Jim Grosbachf922c472011-02-12 01:34:40 +0000827/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000828/// token must be an Identifier when called, and if it is a coprocessor
829/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000830ARMAsmParser::OperandMatchResultTy ARMAsmParser::
831tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000832 SMLoc S = Parser.getTok().getLoc();
833 const AsmToken &Tok = Parser.getTok();
834 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
835
836 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
837 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +0000838 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000839
840 Parser.Lex(); // Eat identifier token.
841 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000842 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000843}
844
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000845/// Parse a register list, return it if successful else return null. The first
846/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000847bool ARMAsmParser::
848ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000849 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000850 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000851 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000852
Bill Wendling7729e062010-11-09 22:44:22 +0000853 // Read the rest of the registers in the list.
854 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000855 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000856
Bill Wendling7729e062010-11-09 22:44:22 +0000857 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000858 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000859 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000860
Sean Callanan18b83232010-01-19 21:44:56 +0000861 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000862 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000863 if (RegTok.isNot(AsmToken::Identifier)) {
864 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000865 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000866 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000867
Bill Wendling1d6a2652010-11-06 10:40:24 +0000868 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000869 if (RegNum == -1) {
870 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000871 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000872 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000873
Bill Wendlinge7176102010-11-06 22:36:58 +0000874 if (IsRange) {
875 int Reg = PrevRegNum;
876 do {
877 ++Reg;
878 Registers.push_back(std::make_pair(Reg, RegLoc));
879 } while (Reg != RegNum);
880 } else {
881 Registers.push_back(std::make_pair(RegNum, RegLoc));
882 }
883
884 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000885 } while (Parser.getTok().is(AsmToken::Comma) ||
886 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000887
888 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000889 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000890 if (RCurlyTok.isNot(AsmToken::RCurly)) {
891 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000892 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000893 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000894
Bill Wendlinge7176102010-11-06 22:36:58 +0000895 SMLoc E = RCurlyTok.getLoc();
896 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000897
Bill Wendlinge7176102010-11-06 22:36:58 +0000898 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000899 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000900 RI = Registers.begin(), RE = Registers.end();
901
Bill Wendling7caebff2011-01-12 21:20:59 +0000902 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000903 bool EmittedWarning = false;
904
Bill Wendling7caebff2011-01-12 21:20:59 +0000905 DenseMap<unsigned, bool> RegMap;
906 RegMap[HighRegNum] = true;
907
Bill Wendlinge7176102010-11-06 22:36:58 +0000908 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000909 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000910 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000911
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000912 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000913 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000914 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000915 }
916
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000917 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000918 Warning(RegInfo.second,
919 "register not in ascending order in register list");
920
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000921 RegMap[Reg] = true;
922 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000923 }
924
Bill Wendling50d0f582010-11-18 23:43:05 +0000925 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
926 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000927}
928
Jim Grosbachf922c472011-02-12 01:34:40 +0000929/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
930ARMAsmParser::OperandMatchResultTy ARMAsmParser::
931tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000932 SMLoc S = Parser.getTok().getLoc();
933 const AsmToken &Tok = Parser.getTok();
934 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
935 StringRef OptStr = Tok.getString();
936
937 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
938 .Case("sy", ARM_MB::SY)
939 .Case("st", ARM_MB::ST)
940 .Case("ish", ARM_MB::ISH)
941 .Case("ishst", ARM_MB::ISHST)
942 .Case("nsh", ARM_MB::NSH)
943 .Case("nshst", ARM_MB::NSHST)
944 .Case("osh", ARM_MB::OSH)
945 .Case("oshst", ARM_MB::OSHST)
946 .Default(~0U);
947
948 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +0000949 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000950
951 Parser.Lex(); // Eat identifier token.
952 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000953 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000954}
955
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000956/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000957ARMAsmParser::OperandMatchResultTy ARMAsmParser::
958tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
959 SMLoc S = Parser.getTok().getLoc();
960 const AsmToken &Tok = Parser.getTok();
961 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
962 StringRef IFlagsStr = Tok.getString();
963
964 unsigned IFlags = 0;
965 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
966 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
967 .Case("a", ARM_PROC::A)
968 .Case("i", ARM_PROC::I)
969 .Case("f", ARM_PROC::F)
970 .Default(~0U);
971
972 // If some specific iflag is already set, it means that some letter is
973 // present more than once, this is not acceptable.
974 if (Flag == ~0U || (IFlags & Flag))
975 return MatchOperand_NoMatch;
976
977 IFlags |= Flag;
978 }
979
980 Parser.Lex(); // Eat identifier token.
981 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
982 return MatchOperand_Success;
983}
984
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000985/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
986ARMAsmParser::OperandMatchResultTy ARMAsmParser::
987tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
988 SMLoc S = Parser.getTok().getLoc();
989 const AsmToken &Tok = Parser.getTok();
990 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
991 StringRef Mask = Tok.getString();
992
993 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
994 size_t Start = 0, Next = Mask.find('_');
995 StringRef Flags = "";
996 StringRef SpecReg = Mask.slice(Start, Next);
997 if (Next != StringRef::npos)
998 Flags = Mask.slice(Next+1, Mask.size());
999
1000 // FlagsVal contains the complete mask:
1001 // 3-0: Mask
1002 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1003 unsigned FlagsVal = 0;
1004
1005 if (SpecReg == "apsr") {
1006 FlagsVal = StringSwitch<unsigned>(Flags)
1007 .Case("nzcvq", 0x8) // same as CPSR_c
1008 .Case("g", 0x4) // same as CPSR_s
1009 .Case("nzcvqg", 0xc) // same as CPSR_fs
1010 .Default(~0U);
1011
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001012 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001013 if (!Flags.empty())
1014 return MatchOperand_NoMatch;
1015 else
1016 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001017 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001018 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
1019 for (int i = 0, e = Flags.size(); i != e; ++i) {
1020 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1021 .Case("c", 1)
1022 .Case("x", 2)
1023 .Case("s", 4)
1024 .Case("f", 8)
1025 .Default(~0U);
1026
1027 // If some specific flag is already set, it means that some letter is
1028 // present more than once, this is not acceptable.
1029 if (FlagsVal == ~0U || (FlagsVal & Flag))
1030 return MatchOperand_NoMatch;
1031 FlagsVal |= Flag;
1032 }
1033 } else // No match for special register.
1034 return MatchOperand_NoMatch;
1035
1036 // Special register without flags are equivalent to "fc" flags.
1037 if (!FlagsVal)
1038 FlagsVal = 0x9;
1039
1040 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1041 if (SpecReg == "spsr")
1042 FlagsVal |= 16;
1043
1044 Parser.Lex(); // Eat identifier token.
1045 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1046 return MatchOperand_Success;
1047}
1048
Bill Wendlinge7176102010-11-06 22:36:58 +00001049/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001050/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001051///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001052/// TODO Only preindexing and postindexing addressing are started, unindexed
1053/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001054bool ARMAsmParser::
1055ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +00001056 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001057 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001058 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001059 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001060 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001061
Sean Callanan18b83232010-01-19 21:44:56 +00001062 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001063 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1064 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001065 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001066 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001067 int BaseRegNum = TryParseRegister();
1068 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001069 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001070 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001071 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001072
Daniel Dunbar05710932011-01-18 05:34:17 +00001073 // The next token must either be a comma or a closing bracket.
1074 const AsmToken &Tok = Parser.getTok();
1075 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1076 return true;
1077
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001078 bool Preindexed = false;
1079 bool Postindexed = false;
1080 bool OffsetIsReg = false;
1081 bool Negative = false;
1082 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001083 ARMOperand *WBOp = 0;
1084 int OffsetRegNum = -1;
1085 bool OffsetRegShifted = false;
1086 enum ShiftType ShiftType = Lsl;
1087 const MCExpr *ShiftAmount = 0;
1088 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001089
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001090 // First look for preindexed address forms, that is after the "[Rn" we now
1091 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001092 if (Tok.is(AsmToken::Comma)) {
1093 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001094 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001095
Chris Lattner550276e2010-10-28 20:52:15 +00001096 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1097 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001098 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001099 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001100 if (RBracTok.isNot(AsmToken::RBrac)) {
1101 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001102 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001103 }
Sean Callanan76264762010-04-02 22:27:05 +00001104 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001105 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001106
Sean Callanan18b83232010-01-19 21:44:56 +00001107 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001108 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001109 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1110 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001111 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001112 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001113 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001114 } else {
1115 // The "[Rn" we have so far was not followed by a comma.
1116
Jim Grosbach80eb2332010-10-29 17:41:25 +00001117 // If there's anything other than the right brace, this is a post indexing
1118 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001119 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001120 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001121
Sean Callanan18b83232010-01-19 21:44:56 +00001122 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001123
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001124 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001125 Postindexed = true;
1126 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001127
Chris Lattner550276e2010-10-28 20:52:15 +00001128 if (NextTok.isNot(AsmToken::Comma)) {
1129 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001130 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001131 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001132
Sean Callananb9a25b72010-01-19 20:27:46 +00001133 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001134
Chris Lattner550276e2010-10-28 20:52:15 +00001135 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001136 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001137 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001138 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001139 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001140 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001141
1142 // Force Offset to exist if used.
1143 if (!OffsetIsReg) {
1144 if (!Offset)
1145 Offset = MCConstantExpr::Create(0, getContext());
1146 }
1147
1148 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
1149 OffsetRegNum, OffsetRegShifted,
1150 ShiftType, ShiftAmount, Preindexed,
1151 Postindexed, Negative, Writeback,
1152 S, E));
1153 if (WBOp)
1154 Operands.push_back(WBOp);
1155
1156 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001157}
1158
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001159/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1160/// we will parse the following (were +/- means that a plus or minus is
1161/// optional):
1162/// +/-Rm
1163/// +/-Rm, shift
1164/// #offset
1165/// we return false on success or an error otherwise.
1166bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001167 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001168 enum ShiftType &ShiftType,
1169 const MCExpr *&ShiftAmount,
1170 const MCExpr *&Offset,
1171 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001172 int &OffsetRegNum,
1173 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001174 Negative = false;
1175 OffsetRegShifted = false;
1176 OffsetIsReg = false;
1177 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001178 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001179 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001180 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001181 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001182 else if (NextTok.is(AsmToken::Minus)) {
1183 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001184 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001185 }
1186 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001187 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001188 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001189 SMLoc CurLoc = OffsetRegTok.getLoc();
1190 OffsetRegNum = TryParseRegister();
1191 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001192 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001193 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001194 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001195 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001196
Bill Wendling12f40e92010-11-06 10:51:53 +00001197 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001198 if (OffsetRegNum != -1) {
1199 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001200 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001201 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001202 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001203
Sean Callanan18b83232010-01-19 21:44:56 +00001204 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001205 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001206 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001207 OffsetRegShifted = true;
1208 }
1209 }
1210 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1211 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001212 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001213 if (HashTok.isNot(AsmToken::Hash))
1214 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001215
Sean Callananb9a25b72010-01-19 20:27:46 +00001216 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001217
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001218 if (getParser().ParseExpression(Offset))
1219 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001220 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001221 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001222 return false;
1223}
1224
1225/// ParseShift as one of these two:
1226/// ( lsl | lsr | asr | ror ) , # shift_amount
1227/// rrx
1228/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +00001229bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +00001230 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001231 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001232 if (Tok.isNot(AsmToken::Identifier))
1233 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001234 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001235 if (ShiftName == "lsl" || ShiftName == "LSL")
1236 St = Lsl;
1237 else if (ShiftName == "lsr" || ShiftName == "LSR")
1238 St = Lsr;
1239 else if (ShiftName == "asr" || ShiftName == "ASR")
1240 St = Asr;
1241 else if (ShiftName == "ror" || ShiftName == "ROR")
1242 St = Ror;
1243 else if (ShiftName == "rrx" || ShiftName == "RRX")
1244 St = Rrx;
1245 else
1246 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001247 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001248
1249 // Rrx stands alone.
1250 if (St == Rrx)
1251 return false;
1252
1253 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001254 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001255 if (HashTok.isNot(AsmToken::Hash))
1256 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001257 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001258
1259 if (getParser().ParseExpression(ShiftAmount))
1260 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001261
1262 return false;
1263}
1264
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001265/// Parse a arm instruction operand. For now this parses the operand regardless
1266/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001267bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001268 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001269 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001270
1271 // Check if the current operand has a custom associated parser, if so, try to
1272 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001273 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1274 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001275 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001276 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1277 // there was a match, but an error occurred, in which case, just return that
1278 // the operand parsing failed.
1279 if (ResTy == MatchOperand_ParseFail)
1280 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001281
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001282 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001283 default:
1284 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001285 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001286 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001287 if (!TryParseRegisterWithWriteBack(Operands))
1288 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001289
1290 // Fall though for the Identifier case that is not a register or a
1291 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001292 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1293 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001294 // This was not a register so parse other operands that start with an
1295 // identifier (like labels) as expressions and create them as immediates.
1296 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001297 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001298 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001299 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001300 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001301 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1302 return false;
1303 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001304 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001305 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001306 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001307 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001308 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001309 // #42 -> immediate.
1310 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001311 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001312 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001313 const MCExpr *ImmVal;
1314 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001315 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001316 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001317 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1318 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001319 case AsmToken::Colon: {
1320 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001321 // FIXME: Check it's an expression prefix,
1322 // e.g. (FOO - :lower16:BAR) isn't legal.
1323 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001324 if (ParsePrefix(RefKind))
1325 return true;
1326
Evan Cheng75972122011-01-13 07:58:56 +00001327 const MCExpr *SubExprVal;
1328 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001329 return true;
1330
Evan Cheng75972122011-01-13 07:58:56 +00001331 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1332 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001333 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001334 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001335 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001336 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001337 }
1338}
1339
Evan Cheng75972122011-01-13 07:58:56 +00001340// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1341// :lower16: and :upper16:.
1342bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1343 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001344
1345 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001346 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001347 Parser.Lex(); // Eat ':'
1348
1349 if (getLexer().isNot(AsmToken::Identifier)) {
1350 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1351 return true;
1352 }
1353
1354 StringRef IDVal = Parser.getTok().getIdentifier();
1355 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001356 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001357 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001358 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001359 } else {
1360 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1361 return true;
1362 }
1363 Parser.Lex();
1364
1365 if (getLexer().isNot(AsmToken::Colon)) {
1366 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1367 return true;
1368 }
1369 Parser.Lex(); // Eat the last ':'
1370 return false;
1371}
1372
1373const MCExpr *
1374ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1375 MCSymbolRefExpr::VariantKind Variant) {
1376 // Recurse over the given expression, rebuilding it to apply the given variant
1377 // to the leftmost symbol.
1378 if (Variant == MCSymbolRefExpr::VK_None)
1379 return E;
1380
1381 switch (E->getKind()) {
1382 case MCExpr::Target:
1383 llvm_unreachable("Can't handle target expr yet");
1384 case MCExpr::Constant:
1385 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1386
1387 case MCExpr::SymbolRef: {
1388 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1389
1390 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1391 return 0;
1392
1393 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1394 }
1395
1396 case MCExpr::Unary:
1397 llvm_unreachable("Can't handle unary expressions yet");
1398
1399 case MCExpr::Binary: {
1400 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1401 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1402 const MCExpr *RHS = BE->getRHS();
1403 if (!LHS)
1404 return 0;
1405
1406 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1407 }
1408 }
1409
1410 assert(0 && "Invalid expression kind!");
1411 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001412}
1413
Daniel Dunbar352e1482011-01-11 15:59:50 +00001414/// \brief Given a mnemonic, split out possible predication code and carry
1415/// setting letters to form a canonical mnemonic and flags.
1416//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001417// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001418static StringRef SplitMnemonic(StringRef Mnemonic,
1419 unsigned &PredicationCode,
1420 bool &CarrySetting,
1421 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001422 PredicationCode = ARMCC::AL;
1423 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001424 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001425
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001426 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001427 //
1428 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001429 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1430 Mnemonic == "movs" ||
1431 Mnemonic == "svc" ||
1432 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1433 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1434 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1435 Mnemonic == "vclt" ||
1436 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1437 Mnemonic == "vcle" ||
1438 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1439 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1440 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001441 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001442
Daniel Dunbar352e1482011-01-11 15:59:50 +00001443 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001444 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001445 .Case("eq", ARMCC::EQ)
1446 .Case("ne", ARMCC::NE)
1447 .Case("hs", ARMCC::HS)
1448 .Case("lo", ARMCC::LO)
1449 .Case("mi", ARMCC::MI)
1450 .Case("pl", ARMCC::PL)
1451 .Case("vs", ARMCC::VS)
1452 .Case("vc", ARMCC::VC)
1453 .Case("hi", ARMCC::HI)
1454 .Case("ls", ARMCC::LS)
1455 .Case("ge", ARMCC::GE)
1456 .Case("lt", ARMCC::LT)
1457 .Case("gt", ARMCC::GT)
1458 .Case("le", ARMCC::LE)
1459 .Case("al", ARMCC::AL)
1460 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001461 if (CC != ~0U) {
1462 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001463 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001464 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001465
Daniel Dunbar352e1482011-01-11 15:59:50 +00001466 // Next, determine if we have a carry setting bit. We explicitly ignore all
1467 // the instructions we know end in 's'.
1468 if (Mnemonic.endswith("s") &&
1469 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1470 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1471 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1472 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1473 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1474 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1475 CarrySetting = true;
1476 }
1477
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001478 // The "cps" instruction can have a interrupt mode operand which is glued into
1479 // the mnemonic. Check if this is the case, split it and parse the imod op
1480 if (Mnemonic.startswith("cps")) {
1481 // Split out any imod code.
1482 unsigned IMod =
1483 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1484 .Case("ie", ARM_PROC::IE)
1485 .Case("id", ARM_PROC::ID)
1486 .Default(~0U);
1487 if (IMod != ~0U) {
1488 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1489 ProcessorIMod = IMod;
1490 }
1491 }
1492
Daniel Dunbar352e1482011-01-11 15:59:50 +00001493 return Mnemonic;
1494}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001495
1496/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1497/// inclusion of carry set or predication code operands.
1498//
1499// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001500void ARMAsmParser::
1501GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1502 bool &CanAcceptPredicationCode) {
1503 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1504
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001505 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1506 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1507 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1508 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1509 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1510 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1511 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1512 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1513 CanAcceptCarrySet = true;
1514 } else {
1515 CanAcceptCarrySet = false;
1516 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001517
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001518 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1519 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1520 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1521 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001522 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001523 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001524 CanAcceptPredicationCode = false;
1525 } else {
1526 CanAcceptPredicationCode = true;
1527 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001528
1529 if (isThumb)
1530 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001531 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001532 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001533}
1534
1535/// Parse an arm instruction mnemonic followed by its operands.
1536bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1537 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1538 // Create the leading tokens for the mnemonic, split by '.' characters.
1539 size_t Start = 0, Next = Name.find('.');
1540 StringRef Head = Name.slice(Start, Next);
1541
Daniel Dunbar352e1482011-01-11 15:59:50 +00001542 // Split out the predication code and carry setting flag from the mnemonic.
1543 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001544 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001545 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001546 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1547 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001548
Chris Lattner3a697562010-10-28 17:20:03 +00001549 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001550
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001551 // Next, add the CCOut and ConditionCode operands, if needed.
1552 //
1553 // For mnemonics which can ever incorporate a carry setting bit or predication
1554 // code, our matching model involves us always generating CCOut and
1555 // ConditionCode operands to match the mnemonic "as written" and then we let
1556 // the matcher deal with finding the right instruction or generating an
1557 // appropriate error.
1558 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1559 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1560
1561 // Add the carry setting operand, if necessary.
1562 //
1563 // FIXME: It would be awesome if we could somehow invent a location such that
1564 // match errors on this operand would print a nice diagnostic about how the
1565 // 's' character in the mnemonic resulted in a CCOut operand.
1566 if (CanAcceptCarrySet) {
1567 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1568 NameLoc));
1569 } else {
1570 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1571 // misspelled another mnemonic).
1572
1573 // FIXME: Issue a nice error.
1574 }
1575
1576 // Add the predication code operand, if necessary.
1577 if (CanAcceptPredicationCode) {
1578 Operands.push_back(ARMOperand::CreateCondCode(
1579 ARMCC::CondCodes(PredicationCode), NameLoc));
1580 } else {
1581 // This mnemonic can't ever accept a predication code, but the user wrote
1582 // one (or misspelled another mnemonic).
1583
1584 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001585 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001586
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001587 // Add the processor imod operand, if necessary.
1588 if (ProcessorIMod) {
1589 Operands.push_back(ARMOperand::CreateImm(
1590 MCConstantExpr::Create(ProcessorIMod, getContext()),
1591 NameLoc, NameLoc));
1592 } else {
1593 // This mnemonic can't ever accept a imod, but the user wrote
1594 // one (or misspelled another mnemonic).
1595
1596 // FIXME: Issue a nice error.
1597 }
1598
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001599 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001600 while (Next != StringRef::npos) {
1601 Start = Next;
1602 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001603 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001604
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001605 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001606 }
1607
1608 // Read the remaining operands.
1609 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001610 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001611 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001612 Parser.EatToEndOfStatement();
1613 return true;
1614 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001615
1616 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001617 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001618
1619 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001620 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001621 Parser.EatToEndOfStatement();
1622 return true;
1623 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001624 }
1625 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001626
Chris Lattnercbf8a982010-09-11 16:18:25 +00001627 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1628 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001629 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001630 }
Bill Wendling146018f2010-11-06 21:42:12 +00001631
Chris Lattner34e53142010-09-08 05:10:46 +00001632 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001633 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001634}
1635
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001636bool ARMAsmParser::
1637MatchAndEmitInstruction(SMLoc IDLoc,
1638 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1639 MCStreamer &Out) {
1640 MCInst Inst;
1641 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001642 MatchResultTy MatchResult, MatchResult2;
1643 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1644 if (MatchResult != Match_Success) {
1645 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1646 // that does not update the condition codes. So try adding a CCOut operand
1647 // with a value of reg0.
1648 if (MatchResult == Match_InvalidOperand) {
1649 Operands.insert(Operands.begin() + 1,
1650 ARMOperand::CreateCCOut(0,
1651 ((ARMOperand*)Operands[0])->getStartLoc()));
1652 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1653 if (MatchResult2 == Match_Success)
1654 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001655 else {
1656 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001657 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001658 delete CCOut;
1659 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001660 }
1661 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1662 // that updates the condition codes if it ends in 's'. So see if the
1663 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1664 // operand with a value of CPSR.
1665 else if(MatchResult == Match_MnemonicFail) {
1666 // Get the instruction mnemonic, which is the first token.
1667 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1668 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1669 // removed the 's' from the mnemonic for matching.
1670 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1671 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001672 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1673 Operands.erase(Operands.begin());
1674 delete OldMnemonic;
1675 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001676 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1677 Operands.insert(Operands.begin() + 1,
1678 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1679 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1680 if (MatchResult2 == Match_Success)
1681 MatchResult = Match_Success;
1682 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001683 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1684 Operands.erase(Operands.begin());
1685 delete OldMnemonic;
1686 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001687 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001688 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1689 Operands.erase(Operands.begin() + 1);
1690 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001691 }
1692 }
1693 }
1694 }
1695 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001696 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001697 Out.EmitInstruction(Inst);
1698 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001699 case Match_MissingFeature:
1700 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1701 return true;
1702 case Match_InvalidOperand: {
1703 SMLoc ErrorLoc = IDLoc;
1704 if (ErrorInfo != ~0U) {
1705 if (ErrorInfo >= Operands.size())
1706 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001707
Chris Lattnere73d4f82010-10-28 21:41:58 +00001708 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1709 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1710 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001711
Chris Lattnere73d4f82010-10-28 21:41:58 +00001712 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001713 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001714 case Match_MnemonicFail:
1715 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00001716 case Match_ConversionFail:
1717 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00001718 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001719
Eric Christopherc223e2b2010-10-29 09:26:59 +00001720 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001721 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001722}
1723
Kevin Enderby515d5092009-10-15 20:48:48 +00001724/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001725bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1726 StringRef IDVal = DirectiveID.getIdentifier();
1727 if (IDVal == ".word")
1728 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001729 else if (IDVal == ".thumb")
1730 return ParseDirectiveThumb(DirectiveID.getLoc());
1731 else if (IDVal == ".thumb_func")
1732 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1733 else if (IDVal == ".code")
1734 return ParseDirectiveCode(DirectiveID.getLoc());
1735 else if (IDVal == ".syntax")
1736 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001737 return true;
1738}
1739
1740/// ParseDirectiveWord
1741/// ::= .word [ expression (, expression)* ]
1742bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1743 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1744 for (;;) {
1745 const MCExpr *Value;
1746 if (getParser().ParseExpression(Value))
1747 return true;
1748
Chris Lattneraaec2052010-01-19 19:46:13 +00001749 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001750
1751 if (getLexer().is(AsmToken::EndOfStatement))
1752 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001753
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001754 // FIXME: Improve diagnostic.
1755 if (getLexer().isNot(AsmToken::Comma))
1756 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001757 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001758 }
1759 }
1760
Sean Callananb9a25b72010-01-19 20:27:46 +00001761 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001762 return false;
1763}
1764
Kevin Enderby515d5092009-10-15 20:48:48 +00001765/// ParseDirectiveThumb
1766/// ::= .thumb
1767bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1768 if (getLexer().isNot(AsmToken::EndOfStatement))
1769 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001770 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001771
1772 // TODO: set thumb mode
1773 // TODO: tell the MC streamer the mode
1774 // getParser().getStreamer().Emit???();
1775 return false;
1776}
1777
1778/// ParseDirectiveThumbFunc
1779/// ::= .thumbfunc symbol_name
1780bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001781 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001782 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001783 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001784 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001785 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001786 if (getLexer().isNot(AsmToken::EndOfStatement))
1787 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001788 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001789
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001790 // Mark symbol as a thumb symbol.
1791 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1792 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001793 return false;
1794}
1795
1796/// ParseDirectiveSyntax
1797/// ::= .syntax unified | divided
1798bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001799 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001800 if (Tok.isNot(AsmToken::Identifier))
1801 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001802 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001803 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001804 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001805 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00001806 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00001807 else
1808 return Error(L, "unrecognized syntax mode in .syntax directive");
1809
1810 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001811 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001812 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001813
1814 // TODO tell the MC streamer the mode
1815 // getParser().getStreamer().Emit???();
1816 return false;
1817}
1818
1819/// ParseDirectiveCode
1820/// ::= .code 16 | 32
1821bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001822 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001823 if (Tok.isNot(AsmToken::Integer))
1824 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001825 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001826 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001827 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001828 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001829 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001830 else
1831 return Error(L, "invalid operand to .code directive");
1832
1833 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001834 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001835 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001836
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001837 // FIXME: We need to be able switch subtargets at this point so that
1838 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1839 // includes Feature_IsThumb or not to match the right instructions. This is
1840 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1841 if (Val == 16){
1842 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1843 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001844 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001845 }
1846 else{
1847 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1848 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001849 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001850 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001851
Kevin Enderby515d5092009-10-15 20:48:48 +00001852 return false;
1853}
1854
Sean Callanan90b70972010-04-07 20:29:34 +00001855extern "C" void LLVMInitializeARMAsmLexer();
1856
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001857/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001858extern "C" void LLVMInitializeARMAsmParser() {
1859 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1860 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001861 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001862}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001863
Chris Lattner0692ee62010-09-06 19:11:01 +00001864#define GET_REGISTER_MATCHER
1865#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001866#include "ARMGenAsmMatcher.inc"