blob: 9bde90b104081cc703064a90eff4eb506dcfb505 [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
1012 if (FlagsVal == ~0U)
1013 if (!Flags.empty())
1014 return MatchOperand_NoMatch;
1015 else
1016 FlagsVal = 0; // No flag
1017 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
1018 for (int i = 0, e = Flags.size(); i != e; ++i) {
1019 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1020 .Case("c", 1)
1021 .Case("x", 2)
1022 .Case("s", 4)
1023 .Case("f", 8)
1024 .Default(~0U);
1025
1026 // If some specific flag is already set, it means that some letter is
1027 // present more than once, this is not acceptable.
1028 if (FlagsVal == ~0U || (FlagsVal & Flag))
1029 return MatchOperand_NoMatch;
1030 FlagsVal |= Flag;
1031 }
1032 } else // No match for special register.
1033 return MatchOperand_NoMatch;
1034
1035 // Special register without flags are equivalent to "fc" flags.
1036 if (!FlagsVal)
1037 FlagsVal = 0x9;
1038
1039 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1040 if (SpecReg == "spsr")
1041 FlagsVal |= 16;
1042
1043 Parser.Lex(); // Eat identifier token.
1044 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1045 return MatchOperand_Success;
1046}
1047
Bill Wendlinge7176102010-11-06 22:36:58 +00001048/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001049/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001050///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001051/// TODO Only preindexing and postindexing addressing are started, unindexed
1052/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001053bool ARMAsmParser::
1054ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +00001055 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001056 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001057 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001058 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001059 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001060
Sean Callanan18b83232010-01-19 21:44:56 +00001061 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001062 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1063 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001064 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001065 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001066 int BaseRegNum = TryParseRegister();
1067 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001068 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001069 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001070 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001071
Daniel Dunbar05710932011-01-18 05:34:17 +00001072 // The next token must either be a comma or a closing bracket.
1073 const AsmToken &Tok = Parser.getTok();
1074 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1075 return true;
1076
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001077 bool Preindexed = false;
1078 bool Postindexed = false;
1079 bool OffsetIsReg = false;
1080 bool Negative = false;
1081 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001082 ARMOperand *WBOp = 0;
1083 int OffsetRegNum = -1;
1084 bool OffsetRegShifted = false;
1085 enum ShiftType ShiftType = Lsl;
1086 const MCExpr *ShiftAmount = 0;
1087 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001088
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001089 // First look for preindexed address forms, that is after the "[Rn" we now
1090 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001091 if (Tok.is(AsmToken::Comma)) {
1092 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001093 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001094
Chris Lattner550276e2010-10-28 20:52:15 +00001095 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1096 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001097 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001098 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001099 if (RBracTok.isNot(AsmToken::RBrac)) {
1100 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001101 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001102 }
Sean Callanan76264762010-04-02 22:27:05 +00001103 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001104 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001105
Sean Callanan18b83232010-01-19 21:44:56 +00001106 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001107 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001108 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1109 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001110 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001111 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001112 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001113 } else {
1114 // The "[Rn" we have so far was not followed by a comma.
1115
Jim Grosbach80eb2332010-10-29 17:41:25 +00001116 // If there's anything other than the right brace, this is a post indexing
1117 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001118 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001119 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001120
Sean Callanan18b83232010-01-19 21:44:56 +00001121 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001122
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001123 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001124 Postindexed = true;
1125 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001126
Chris Lattner550276e2010-10-28 20:52:15 +00001127 if (NextTok.isNot(AsmToken::Comma)) {
1128 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001129 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001130 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001131
Sean Callananb9a25b72010-01-19 20:27:46 +00001132 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001133
Chris Lattner550276e2010-10-28 20:52:15 +00001134 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001135 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001136 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001137 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001138 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001139 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001140
1141 // Force Offset to exist if used.
1142 if (!OffsetIsReg) {
1143 if (!Offset)
1144 Offset = MCConstantExpr::Create(0, getContext());
1145 }
1146
1147 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
1148 OffsetRegNum, OffsetRegShifted,
1149 ShiftType, ShiftAmount, Preindexed,
1150 Postindexed, Negative, Writeback,
1151 S, E));
1152 if (WBOp)
1153 Operands.push_back(WBOp);
1154
1155 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001156}
1157
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001158/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1159/// we will parse the following (were +/- means that a plus or minus is
1160/// optional):
1161/// +/-Rm
1162/// +/-Rm, shift
1163/// #offset
1164/// we return false on success or an error otherwise.
1165bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001166 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001167 enum ShiftType &ShiftType,
1168 const MCExpr *&ShiftAmount,
1169 const MCExpr *&Offset,
1170 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001171 int &OffsetRegNum,
1172 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001173 Negative = false;
1174 OffsetRegShifted = false;
1175 OffsetIsReg = false;
1176 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001177 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001178 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001179 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001180 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001181 else if (NextTok.is(AsmToken::Minus)) {
1182 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001183 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001184 }
1185 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001186 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001187 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001188 SMLoc CurLoc = OffsetRegTok.getLoc();
1189 OffsetRegNum = TryParseRegister();
1190 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001191 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001192 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001193 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001194 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001195
Bill Wendling12f40e92010-11-06 10:51:53 +00001196 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001197 if (OffsetRegNum != -1) {
1198 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001199 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001200 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001201 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001202
Sean Callanan18b83232010-01-19 21:44:56 +00001203 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001204 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001205 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001206 OffsetRegShifted = true;
1207 }
1208 }
1209 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1210 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001211 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001212 if (HashTok.isNot(AsmToken::Hash))
1213 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001214
Sean Callananb9a25b72010-01-19 20:27:46 +00001215 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001216
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001217 if (getParser().ParseExpression(Offset))
1218 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001219 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001220 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001221 return false;
1222}
1223
1224/// ParseShift as one of these two:
1225/// ( lsl | lsr | asr | ror ) , # shift_amount
1226/// rrx
1227/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +00001228bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +00001229 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001230 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001231 if (Tok.isNot(AsmToken::Identifier))
1232 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001233 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001234 if (ShiftName == "lsl" || ShiftName == "LSL")
1235 St = Lsl;
1236 else if (ShiftName == "lsr" || ShiftName == "LSR")
1237 St = Lsr;
1238 else if (ShiftName == "asr" || ShiftName == "ASR")
1239 St = Asr;
1240 else if (ShiftName == "ror" || ShiftName == "ROR")
1241 St = Ror;
1242 else if (ShiftName == "rrx" || ShiftName == "RRX")
1243 St = Rrx;
1244 else
1245 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001246 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001247
1248 // Rrx stands alone.
1249 if (St == Rrx)
1250 return false;
1251
1252 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001253 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001254 if (HashTok.isNot(AsmToken::Hash))
1255 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001256 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001257
1258 if (getParser().ParseExpression(ShiftAmount))
1259 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001260
1261 return false;
1262}
1263
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001264/// Parse a arm instruction operand. For now this parses the operand regardless
1265/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001266bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001267 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001268 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001269
1270 // Check if the current operand has a custom associated parser, if so, try to
1271 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001272 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1273 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001274 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001275 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1276 // there was a match, but an error occurred, in which case, just return that
1277 // the operand parsing failed.
1278 if (ResTy == MatchOperand_ParseFail)
1279 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001280
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001281 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001282 default:
1283 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001284 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001285 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001286 if (!TryParseRegisterWithWriteBack(Operands))
1287 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001288
1289 // Fall though for the Identifier case that is not a register or a
1290 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001291 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1292 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001293 // This was not a register so parse other operands that start with an
1294 // identifier (like labels) as expressions and create them as immediates.
1295 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001296 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001297 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001298 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001299 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001300 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1301 return false;
1302 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001303 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001304 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001305 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001306 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001307 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001308 // #42 -> immediate.
1309 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001310 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001311 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001312 const MCExpr *ImmVal;
1313 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001314 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001315 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001316 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1317 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001318 case AsmToken::Colon: {
1319 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001320 // FIXME: Check it's an expression prefix,
1321 // e.g. (FOO - :lower16:BAR) isn't legal.
1322 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001323 if (ParsePrefix(RefKind))
1324 return true;
1325
Evan Cheng75972122011-01-13 07:58:56 +00001326 const MCExpr *SubExprVal;
1327 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001328 return true;
1329
Evan Cheng75972122011-01-13 07:58:56 +00001330 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1331 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001332 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001333 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001334 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001335 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001336 }
1337}
1338
Evan Cheng75972122011-01-13 07:58:56 +00001339// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1340// :lower16: and :upper16:.
1341bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1342 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001343
1344 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001345 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001346 Parser.Lex(); // Eat ':'
1347
1348 if (getLexer().isNot(AsmToken::Identifier)) {
1349 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1350 return true;
1351 }
1352
1353 StringRef IDVal = Parser.getTok().getIdentifier();
1354 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001355 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001356 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001357 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001358 } else {
1359 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1360 return true;
1361 }
1362 Parser.Lex();
1363
1364 if (getLexer().isNot(AsmToken::Colon)) {
1365 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1366 return true;
1367 }
1368 Parser.Lex(); // Eat the last ':'
1369 return false;
1370}
1371
1372const MCExpr *
1373ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1374 MCSymbolRefExpr::VariantKind Variant) {
1375 // Recurse over the given expression, rebuilding it to apply the given variant
1376 // to the leftmost symbol.
1377 if (Variant == MCSymbolRefExpr::VK_None)
1378 return E;
1379
1380 switch (E->getKind()) {
1381 case MCExpr::Target:
1382 llvm_unreachable("Can't handle target expr yet");
1383 case MCExpr::Constant:
1384 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1385
1386 case MCExpr::SymbolRef: {
1387 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1388
1389 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1390 return 0;
1391
1392 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1393 }
1394
1395 case MCExpr::Unary:
1396 llvm_unreachable("Can't handle unary expressions yet");
1397
1398 case MCExpr::Binary: {
1399 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1400 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1401 const MCExpr *RHS = BE->getRHS();
1402 if (!LHS)
1403 return 0;
1404
1405 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1406 }
1407 }
1408
1409 assert(0 && "Invalid expression kind!");
1410 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001411}
1412
Daniel Dunbar352e1482011-01-11 15:59:50 +00001413/// \brief Given a mnemonic, split out possible predication code and carry
1414/// setting letters to form a canonical mnemonic and flags.
1415//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001416// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001417static StringRef SplitMnemonic(StringRef Mnemonic,
1418 unsigned &PredicationCode,
1419 bool &CarrySetting,
1420 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001421 PredicationCode = ARMCC::AL;
1422 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001423 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001424
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001425 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001426 //
1427 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001428 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1429 Mnemonic == "movs" ||
1430 Mnemonic == "svc" ||
1431 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1432 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1433 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1434 Mnemonic == "vclt" ||
1435 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1436 Mnemonic == "vcle" ||
1437 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1438 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1439 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001440 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001441
Daniel Dunbar352e1482011-01-11 15:59:50 +00001442 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001443 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001444 .Case("eq", ARMCC::EQ)
1445 .Case("ne", ARMCC::NE)
1446 .Case("hs", ARMCC::HS)
1447 .Case("lo", ARMCC::LO)
1448 .Case("mi", ARMCC::MI)
1449 .Case("pl", ARMCC::PL)
1450 .Case("vs", ARMCC::VS)
1451 .Case("vc", ARMCC::VC)
1452 .Case("hi", ARMCC::HI)
1453 .Case("ls", ARMCC::LS)
1454 .Case("ge", ARMCC::GE)
1455 .Case("lt", ARMCC::LT)
1456 .Case("gt", ARMCC::GT)
1457 .Case("le", ARMCC::LE)
1458 .Case("al", ARMCC::AL)
1459 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001460 if (CC != ~0U) {
1461 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001462 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001463 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001464
Daniel Dunbar352e1482011-01-11 15:59:50 +00001465 // Next, determine if we have a carry setting bit. We explicitly ignore all
1466 // the instructions we know end in 's'.
1467 if (Mnemonic.endswith("s") &&
1468 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1469 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1470 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1471 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1472 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1473 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1474 CarrySetting = true;
1475 }
1476
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001477 // The "cps" instruction can have a interrupt mode operand which is glued into
1478 // the mnemonic. Check if this is the case, split it and parse the imod op
1479 if (Mnemonic.startswith("cps")) {
1480 // Split out any imod code.
1481 unsigned IMod =
1482 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1483 .Case("ie", ARM_PROC::IE)
1484 .Case("id", ARM_PROC::ID)
1485 .Default(~0U);
1486 if (IMod != ~0U) {
1487 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1488 ProcessorIMod = IMod;
1489 }
1490 }
1491
Daniel Dunbar352e1482011-01-11 15:59:50 +00001492 return Mnemonic;
1493}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001494
1495/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1496/// inclusion of carry set or predication code operands.
1497//
1498// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001499void ARMAsmParser::
1500GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1501 bool &CanAcceptPredicationCode) {
1502 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1503
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001504 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1505 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1506 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1507 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1508 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1509 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1510 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1511 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1512 CanAcceptCarrySet = true;
1513 } else {
1514 CanAcceptCarrySet = false;
1515 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001516
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001517 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1518 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1519 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1520 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001521 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001522 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001523 CanAcceptPredicationCode = false;
1524 } else {
1525 CanAcceptPredicationCode = true;
1526 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001527
1528 if (isThumb)
1529 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001530 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001531 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001532}
1533
1534/// Parse an arm instruction mnemonic followed by its operands.
1535bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1536 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1537 // Create the leading tokens for the mnemonic, split by '.' characters.
1538 size_t Start = 0, Next = Name.find('.');
1539 StringRef Head = Name.slice(Start, Next);
1540
Daniel Dunbar352e1482011-01-11 15:59:50 +00001541 // Split out the predication code and carry setting flag from the mnemonic.
1542 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001543 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001544 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001545 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1546 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001547
Chris Lattner3a697562010-10-28 17:20:03 +00001548 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001549
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001550 // Next, add the CCOut and ConditionCode operands, if needed.
1551 //
1552 // For mnemonics which can ever incorporate a carry setting bit or predication
1553 // code, our matching model involves us always generating CCOut and
1554 // ConditionCode operands to match the mnemonic "as written" and then we let
1555 // the matcher deal with finding the right instruction or generating an
1556 // appropriate error.
1557 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1558 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1559
1560 // Add the carry setting operand, if necessary.
1561 //
1562 // FIXME: It would be awesome if we could somehow invent a location such that
1563 // match errors on this operand would print a nice diagnostic about how the
1564 // 's' character in the mnemonic resulted in a CCOut operand.
1565 if (CanAcceptCarrySet) {
1566 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1567 NameLoc));
1568 } else {
1569 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1570 // misspelled another mnemonic).
1571
1572 // FIXME: Issue a nice error.
1573 }
1574
1575 // Add the predication code operand, if necessary.
1576 if (CanAcceptPredicationCode) {
1577 Operands.push_back(ARMOperand::CreateCondCode(
1578 ARMCC::CondCodes(PredicationCode), NameLoc));
1579 } else {
1580 // This mnemonic can't ever accept a predication code, but the user wrote
1581 // one (or misspelled another mnemonic).
1582
1583 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001584 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001585
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001586 // Add the processor imod operand, if necessary.
1587 if (ProcessorIMod) {
1588 Operands.push_back(ARMOperand::CreateImm(
1589 MCConstantExpr::Create(ProcessorIMod, getContext()),
1590 NameLoc, NameLoc));
1591 } else {
1592 // This mnemonic can't ever accept a imod, but the user wrote
1593 // one (or misspelled another mnemonic).
1594
1595 // FIXME: Issue a nice error.
1596 }
1597
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001598 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001599 while (Next != StringRef::npos) {
1600 Start = Next;
1601 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001602 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001603
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001604 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001605 }
1606
1607 // Read the remaining operands.
1608 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001609 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001610 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001611 Parser.EatToEndOfStatement();
1612 return true;
1613 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001614
1615 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001616 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001617
1618 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001619 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001620 Parser.EatToEndOfStatement();
1621 return true;
1622 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001623 }
1624 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001625
Chris Lattnercbf8a982010-09-11 16:18:25 +00001626 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1627 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001628 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001629 }
Bill Wendling146018f2010-11-06 21:42:12 +00001630
Chris Lattner34e53142010-09-08 05:10:46 +00001631 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001632 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001633}
1634
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001635bool ARMAsmParser::
1636MatchAndEmitInstruction(SMLoc IDLoc,
1637 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1638 MCStreamer &Out) {
1639 MCInst Inst;
1640 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001641 MatchResultTy MatchResult, MatchResult2;
1642 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1643 if (MatchResult != Match_Success) {
1644 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1645 // that does not update the condition codes. So try adding a CCOut operand
1646 // with a value of reg0.
1647 if (MatchResult == Match_InvalidOperand) {
1648 Operands.insert(Operands.begin() + 1,
1649 ARMOperand::CreateCCOut(0,
1650 ((ARMOperand*)Operands[0])->getStartLoc()));
1651 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1652 if (MatchResult2 == Match_Success)
1653 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001654 else {
1655 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001656 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001657 delete CCOut;
1658 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001659 }
1660 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1661 // that updates the condition codes if it ends in 's'. So see if the
1662 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1663 // operand with a value of CPSR.
1664 else if(MatchResult == Match_MnemonicFail) {
1665 // Get the instruction mnemonic, which is the first token.
1666 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1667 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1668 // removed the 's' from the mnemonic for matching.
1669 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1670 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001671 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1672 Operands.erase(Operands.begin());
1673 delete OldMnemonic;
1674 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001675 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1676 Operands.insert(Operands.begin() + 1,
1677 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1678 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1679 if (MatchResult2 == Match_Success)
1680 MatchResult = Match_Success;
1681 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001682 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1683 Operands.erase(Operands.begin());
1684 delete OldMnemonic;
1685 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001686 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001687 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1688 Operands.erase(Operands.begin() + 1);
1689 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001690 }
1691 }
1692 }
1693 }
1694 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001695 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001696 Out.EmitInstruction(Inst);
1697 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001698 case Match_MissingFeature:
1699 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1700 return true;
1701 case Match_InvalidOperand: {
1702 SMLoc ErrorLoc = IDLoc;
1703 if (ErrorInfo != ~0U) {
1704 if (ErrorInfo >= Operands.size())
1705 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001706
Chris Lattnere73d4f82010-10-28 21:41:58 +00001707 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1708 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1709 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001710
Chris Lattnere73d4f82010-10-28 21:41:58 +00001711 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001712 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001713 case Match_MnemonicFail:
1714 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00001715 case Match_ConversionFail:
1716 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00001717 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001718
Eric Christopherc223e2b2010-10-29 09:26:59 +00001719 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001720 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001721}
1722
Kevin Enderby515d5092009-10-15 20:48:48 +00001723/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001724bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1725 StringRef IDVal = DirectiveID.getIdentifier();
1726 if (IDVal == ".word")
1727 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001728 else if (IDVal == ".thumb")
1729 return ParseDirectiveThumb(DirectiveID.getLoc());
1730 else if (IDVal == ".thumb_func")
1731 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1732 else if (IDVal == ".code")
1733 return ParseDirectiveCode(DirectiveID.getLoc());
1734 else if (IDVal == ".syntax")
1735 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001736 return true;
1737}
1738
1739/// ParseDirectiveWord
1740/// ::= .word [ expression (, expression)* ]
1741bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1742 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1743 for (;;) {
1744 const MCExpr *Value;
1745 if (getParser().ParseExpression(Value))
1746 return true;
1747
Chris Lattneraaec2052010-01-19 19:46:13 +00001748 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001749
1750 if (getLexer().is(AsmToken::EndOfStatement))
1751 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001752
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001753 // FIXME: Improve diagnostic.
1754 if (getLexer().isNot(AsmToken::Comma))
1755 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001756 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001757 }
1758 }
1759
Sean Callananb9a25b72010-01-19 20:27:46 +00001760 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001761 return false;
1762}
1763
Kevin Enderby515d5092009-10-15 20:48:48 +00001764/// ParseDirectiveThumb
1765/// ::= .thumb
1766bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1767 if (getLexer().isNot(AsmToken::EndOfStatement))
1768 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001769 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001770
1771 // TODO: set thumb mode
1772 // TODO: tell the MC streamer the mode
1773 // getParser().getStreamer().Emit???();
1774 return false;
1775}
1776
1777/// ParseDirectiveThumbFunc
1778/// ::= .thumbfunc symbol_name
1779bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001780 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001781 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001782 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001783 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001784 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001785 if (getLexer().isNot(AsmToken::EndOfStatement))
1786 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001787 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001788
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001789 // Mark symbol as a thumb symbol.
1790 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1791 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001792 return false;
1793}
1794
1795/// ParseDirectiveSyntax
1796/// ::= .syntax unified | divided
1797bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001798 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001799 if (Tok.isNot(AsmToken::Identifier))
1800 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001801 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001802 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001803 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001804 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00001805 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00001806 else
1807 return Error(L, "unrecognized syntax mode in .syntax directive");
1808
1809 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001810 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001811 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001812
1813 // TODO tell the MC streamer the mode
1814 // getParser().getStreamer().Emit???();
1815 return false;
1816}
1817
1818/// ParseDirectiveCode
1819/// ::= .code 16 | 32
1820bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001821 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001822 if (Tok.isNot(AsmToken::Integer))
1823 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001824 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001825 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001826 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001827 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001828 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001829 else
1830 return Error(L, "invalid operand to .code directive");
1831
1832 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001833 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001834 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001835
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001836 // FIXME: We need to be able switch subtargets at this point so that
1837 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1838 // includes Feature_IsThumb or not to match the right instructions. This is
1839 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1840 if (Val == 16){
1841 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1842 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001843 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001844 }
1845 else{
1846 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1847 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001848 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001849 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001850
Kevin Enderby515d5092009-10-15 20:48:48 +00001851 return false;
1852}
1853
Sean Callanan90b70972010-04-07 20:29:34 +00001854extern "C" void LLVMInitializeARMAsmLexer();
1855
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001856/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001857extern "C" void LLVMInitializeARMAsmParser() {
1858 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1859 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001860 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001861}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001862
Chris Lattner0692ee62010-09-06 19:11:01 +00001863#define GET_REGISTER_MATCHER
1864#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001865#include "ARMGenAsmMatcher.inc"