blob: 089a08c405f4ed6864c739248b81b269db09390e [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(
100 SmallVectorImpl<MCParsedAsmOperand*> &);
101
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000102public:
Daniel Dunbard73ada72010-07-19 00:33:49 +0000103 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +0000104 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
105 // Initialize the set of available features.
106 setAvailableFeatures(ComputeAvailableFeatures(
107 &TM.getSubtarget<ARMSubtarget>()));
108 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000109
Benjamin Kramer38e59892010-07-14 22:38:02 +0000110 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000111 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000112 virtual bool ParseDirective(AsmToken DirectiveID);
113};
Jim Grosbach16c74252010-10-29 14:46:02 +0000114} // end anonymous namespace
115
Chris Lattner3a697562010-10-28 17:20:03 +0000116namespace {
117
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000118/// ARMOperand - Instances of this class represent a parsed ARM machine
119/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000120class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000121 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000122 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000123 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000124 CoprocNum,
125 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000126 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000127 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000128 Memory,
129 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000130 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000131 DPRRegisterList,
132 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000133 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000134 } Kind;
135
Sean Callanan76264762010-04-02 22:27:05 +0000136 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000137 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000138
139 union {
140 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000141 ARMCC::CondCodes Val;
142 } CC;
143
144 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000145 ARM_MB::MemBOpt Val;
146 } MBOpt;
147
148 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000149 unsigned Val;
150 } Cop;
151
152 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000153 const char *Data;
154 unsigned Length;
155 } Tok;
156
157 struct {
158 unsigned RegNum;
159 } Reg;
160
Bill Wendling8155e5b2010-11-06 22:19:43 +0000161 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000162 const MCExpr *Val;
163 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000164
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000165 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000166 struct {
167 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000168 union {
169 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
170 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
171 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000172 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
173 enum ShiftType ShiftType; // used when OffsetRegShifted is true
174 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000175 unsigned Preindexed : 1;
176 unsigned Postindexed : 1;
177 unsigned OffsetIsReg : 1;
178 unsigned Negative : 1; // only used when OffsetIsReg is true
179 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000180 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000181 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000182
Bill Wendling146018f2010-11-06 21:42:12 +0000183 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
184public:
Sean Callanan76264762010-04-02 22:27:05 +0000185 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
186 Kind = o.Kind;
187 StartLoc = o.StartLoc;
188 EndLoc = o.EndLoc;
189 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000190 case CondCode:
191 CC = o.CC;
192 break;
Sean Callanan76264762010-04-02 22:27:05 +0000193 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000194 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000195 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000196 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000197 case Register:
198 Reg = o.Reg;
199 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000200 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000201 case DPRRegisterList:
202 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000203 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000204 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000205 case CoprocNum:
206 case CoprocReg:
207 Cop = o.Cop;
208 break;
Sean Callanan76264762010-04-02 22:27:05 +0000209 case Immediate:
210 Imm = o.Imm;
211 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000212 case MemBarrierOpt:
213 MBOpt = o.MBOpt;
214 break;
Sean Callanan76264762010-04-02 22:27:05 +0000215 case Memory:
216 Mem = o.Mem;
217 break;
218 }
219 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000220
Sean Callanan76264762010-04-02 22:27:05 +0000221 /// getStartLoc - Get the location of the first token of this operand.
222 SMLoc getStartLoc() const { return StartLoc; }
223 /// getEndLoc - Get the location of the last token of this operand.
224 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000225
Daniel Dunbar8462b302010-08-11 06:36:53 +0000226 ARMCC::CondCodes getCondCode() const {
227 assert(Kind == CondCode && "Invalid access!");
228 return CC.Val;
229 }
230
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000231 unsigned getCoproc() const {
232 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
233 return Cop.Val;
234 }
235
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000236 StringRef getToken() const {
237 assert(Kind == Token && "Invalid access!");
238 return StringRef(Tok.Data, Tok.Length);
239 }
240
241 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000242 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000243 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000244 }
245
Bill Wendling5fa22a12010-11-09 23:28:44 +0000246 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000247 assert((Kind == RegisterList || Kind == DPRRegisterList ||
248 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000249 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000250 }
251
Kevin Enderbycfe07242009-10-13 22:19:02 +0000252 const MCExpr *getImm() const {
253 assert(Kind == Immediate && "Invalid access!");
254 return Imm.Val;
255 }
256
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000257 ARM_MB::MemBOpt getMemBarrierOpt() const {
258 assert(Kind == MemBarrierOpt && "Invalid access!");
259 return MBOpt.Val;
260 }
261
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000262 /// @name Memory Operand Accessors
263 /// @{
264
265 unsigned getMemBaseRegNum() const {
266 return Mem.BaseRegNum;
267 }
268 unsigned getMemOffsetRegNum() const {
269 assert(Mem.OffsetIsReg && "Invalid access!");
270 return Mem.Offset.RegNum;
271 }
272 const MCExpr *getMemOffset() const {
273 assert(!Mem.OffsetIsReg && "Invalid access!");
274 return Mem.Offset.Value;
275 }
276 unsigned getMemOffsetRegShifted() const {
277 assert(Mem.OffsetIsReg && "Invalid access!");
278 return Mem.OffsetRegShifted;
279 }
280 const MCExpr *getMemShiftAmount() const {
281 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
282 return Mem.ShiftAmount;
283 }
284 enum ShiftType getMemShiftType() const {
285 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
286 return Mem.ShiftType;
287 }
288 bool getMemPreindexed() const { return Mem.Preindexed; }
289 bool getMemPostindexed() const { return Mem.Postindexed; }
290 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
291 bool getMemNegative() const { return Mem.Negative; }
292 bool getMemWriteback() const { return Mem.Writeback; }
293
294 /// @}
295
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000296 bool isCoprocNum() const { return Kind == CoprocNum; }
297 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000298 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000299 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000300 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000301 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000302 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000303 bool isDPRRegList() const { return Kind == DPRRegisterList; }
304 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000305 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000306 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000307 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000308 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000309 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
310 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000311 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000312
Daniel Dunbar4b462672011-01-18 05:55:27 +0000313 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000314 if (!CE) return false;
315
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000316 // The offset must be a multiple of 4 in the range 0-1020.
317 int64_t Value = CE->getValue();
318 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
319 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000320 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000321 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000322 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000323 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000324 }
325 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000326 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000327 return false;
328
Daniel Dunbar4b462672011-01-18 05:55:27 +0000329 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000330 if (!CE) return false;
331
332 // The offset must be a multiple of 4 in the range 0-124.
333 uint64_t Value = CE->getValue();
334 return ((Value & 0x3) == 0 && Value <= 124);
335 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000336
337 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000338 // Add as immediates when possible. Null MCExpr = 0.
339 if (Expr == 0)
340 Inst.addOperand(MCOperand::CreateImm(0));
341 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000342 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
343 else
344 Inst.addOperand(MCOperand::CreateExpr(Expr));
345 }
346
Daniel Dunbar8462b302010-08-11 06:36:53 +0000347 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000348 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000349 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000350 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
351 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000352 }
353
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000354 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
355 assert(N == 1 && "Invalid number of operands!");
356 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
357 }
358
359 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
360 assert(N == 1 && "Invalid number of operands!");
361 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
362 }
363
Jim Grosbachd67641b2010-12-06 18:21:12 +0000364 void addCCOutOperands(MCInst &Inst, unsigned N) const {
365 assert(N == 1 && "Invalid number of operands!");
366 Inst.addOperand(MCOperand::CreateReg(getReg()));
367 }
368
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000369 void addRegOperands(MCInst &Inst, unsigned N) const {
370 assert(N == 1 && "Invalid number of operands!");
371 Inst.addOperand(MCOperand::CreateReg(getReg()));
372 }
373
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000374 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000375 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000376 const SmallVectorImpl<unsigned> &RegList = getRegList();
377 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000378 I = RegList.begin(), E = RegList.end(); I != E; ++I)
379 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000380 }
381
Bill Wendling0f630752010-11-17 04:32:08 +0000382 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
383 addRegListOperands(Inst, N);
384 }
385
386 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
387 addRegListOperands(Inst, N);
388 }
389
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000390 void addImmOperands(MCInst &Inst, unsigned N) const {
391 assert(N == 1 && "Invalid number of operands!");
392 addExpr(Inst, getImm());
393 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000394
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000395 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
396 assert(N == 1 && "Invalid number of operands!");
397 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
398 }
399
Chris Lattner14b93852010-10-29 00:27:31 +0000400 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
401 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000402
Daniel Dunbar4b462672011-01-18 05:55:27 +0000403 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
404 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000405
Jim Grosbach80eb2332010-10-29 17:41:25 +0000406 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
407 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000408 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000409 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000410
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000411 // The MCInst offset operand doesn't include the low two bits (like
412 // the instruction encoding).
413 int64_t Offset = CE->getValue() / 4;
414 if (Offset >= 0)
415 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
416 Offset)));
417 else
418 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
419 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000420 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000421
Bill Wendlingf4caf692010-12-14 03:36:38 +0000422 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
423 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000424 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
425 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000426 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000427
Bill Wendlingf4caf692010-12-14 03:36:38 +0000428 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
429 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000430 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
431 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000432 assert(CE && "Non-constant mode offset operand!");
433 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000434 }
435
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000436 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000437
Chris Lattner3a697562010-10-28 17:20:03 +0000438 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
439 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000440 Op->CC.Val = CC;
441 Op->StartLoc = S;
442 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000443 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000444 }
445
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000446 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
447 ARMOperand *Op = new ARMOperand(CoprocNum);
448 Op->Cop.Val = CopVal;
449 Op->StartLoc = S;
450 Op->EndLoc = S;
451 return Op;
452 }
453
454 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
455 ARMOperand *Op = new ARMOperand(CoprocReg);
456 Op->Cop.Val = CopVal;
457 Op->StartLoc = S;
458 Op->EndLoc = S;
459 return Op;
460 }
461
Jim Grosbachd67641b2010-12-06 18:21:12 +0000462 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
463 ARMOperand *Op = new ARMOperand(CCOut);
464 Op->Reg.RegNum = RegNum;
465 Op->StartLoc = S;
466 Op->EndLoc = S;
467 return Op;
468 }
469
Chris Lattner3a697562010-10-28 17:20:03 +0000470 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
471 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000472 Op->Tok.Data = Str.data();
473 Op->Tok.Length = Str.size();
474 Op->StartLoc = S;
475 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000476 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000477 }
478
Bill Wendling50d0f582010-11-18 23:43:05 +0000479 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000480 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000481 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000482 Op->StartLoc = S;
483 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000484 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000485 }
486
Bill Wendling7729e062010-11-09 22:44:22 +0000487 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000488 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000489 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000490 KindTy Kind = RegisterList;
491
492 if (ARM::DPRRegClass.contains(Regs.front().first))
493 Kind = DPRRegisterList;
494 else if (ARM::SPRRegClass.contains(Regs.front().first))
495 Kind = SPRRegisterList;
496
497 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000498 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000499 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000500 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000501 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000502 Op->StartLoc = StartLoc;
503 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000504 return Op;
505 }
506
Chris Lattner3a697562010-10-28 17:20:03 +0000507 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
508 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000509 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000510 Op->StartLoc = S;
511 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000512 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000513 }
514
Chris Lattner3a697562010-10-28 17:20:03 +0000515 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000516 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000517 bool OffsetRegShifted, enum ShiftType ShiftType,
518 const MCExpr *ShiftAmount, bool Preindexed,
519 bool Postindexed, bool Negative, bool Writeback,
520 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000521 assert((OffsetRegNum == -1 || OffsetIsReg) &&
522 "OffsetRegNum must imply OffsetIsReg!");
523 assert((!OffsetRegShifted || OffsetIsReg) &&
524 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000525 assert((Offset || OffsetIsReg) &&
526 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000527 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
528 "Cannot have shift amount without shifted register offset!");
529 assert((!Offset || !OffsetIsReg) &&
530 "Cannot have expression offset and register offset!");
531
Chris Lattner3a697562010-10-28 17:20:03 +0000532 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000533 Op->Mem.BaseRegNum = BaseRegNum;
534 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000535 if (OffsetIsReg)
536 Op->Mem.Offset.RegNum = OffsetRegNum;
537 else
538 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000539 Op->Mem.OffsetRegShifted = OffsetRegShifted;
540 Op->Mem.ShiftType = ShiftType;
541 Op->Mem.ShiftAmount = ShiftAmount;
542 Op->Mem.Preindexed = Preindexed;
543 Op->Mem.Postindexed = Postindexed;
544 Op->Mem.Negative = Negative;
545 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000546
Sean Callanan76264762010-04-02 22:27:05 +0000547 Op->StartLoc = S;
548 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000549 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000550 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000551
552 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
553 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
554 Op->MBOpt.Val = Opt;
555 Op->StartLoc = S;
556 Op->EndLoc = S;
557 return Op;
558 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000559};
560
561} // end anonymous namespace.
562
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000563void ARMOperand::dump(raw_ostream &OS) const {
564 switch (Kind) {
565 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000566 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000567 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000568 case CCOut:
569 OS << "<ccout " << getReg() << ">";
570 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000571 case CoprocNum:
572 OS << "<coprocessor number: " << getCoproc() << ">";
573 break;
574 case CoprocReg:
575 OS << "<coprocessor register: " << getCoproc() << ">";
576 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000577 case Immediate:
578 getImm()->print(OS);
579 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000580 case MemBarrierOpt:
581 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
582 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000583 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000584 OS << "<memory "
585 << "base:" << getMemBaseRegNum();
586 if (getMemOffsetIsReg()) {
587 OS << " offset:<register " << getMemOffsetRegNum();
588 if (getMemOffsetRegShifted()) {
589 OS << " offset-shift-type:" << getMemShiftType();
590 OS << " offset-shift-amount:" << *getMemShiftAmount();
591 }
592 } else {
593 OS << " offset:" << *getMemOffset();
594 }
595 if (getMemOffsetIsReg())
596 OS << " (offset-is-reg)";
597 if (getMemPreindexed())
598 OS << " (pre-indexed)";
599 if (getMemPostindexed())
600 OS << " (post-indexed)";
601 if (getMemNegative())
602 OS << " (negative)";
603 if (getMemWriteback())
604 OS << " (writeback)";
605 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000606 break;
607 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000608 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000609 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000610 case RegisterList:
611 case DPRRegisterList:
612 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000613 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000614
Bill Wendling5fa22a12010-11-09 23:28:44 +0000615 const SmallVectorImpl<unsigned> &RegList = getRegList();
616 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000617 I = RegList.begin(), E = RegList.end(); I != E; ) {
618 OS << *I;
619 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000620 }
621
622 OS << ">";
623 break;
624 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000625 case Token:
626 OS << "'" << getToken() << "'";
627 break;
628 }
629}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000630
631/// @name Auto-generated Match Functions
632/// {
633
634static unsigned MatchRegisterName(StringRef Name);
635
636/// }
637
Bob Wilson69df7232011-02-03 21:46:10 +0000638bool ARMAsmParser::ParseRegister(unsigned &RegNo,
639 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000640 RegNo = TryParseRegister();
641
642 return (RegNo == (unsigned)-1);
643}
644
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000645/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000646/// and if it is a register name the token is eaten and the register number is
647/// returned. Otherwise return -1.
648///
649int ARMAsmParser::TryParseRegister() {
650 const AsmToken &Tok = Parser.getTok();
651 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000652
Chris Lattnere5658fa2010-10-30 04:09:10 +0000653 // FIXME: Validate register for the current architecture; we have to do
654 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000655 std::string upperCase = Tok.getString().str();
656 std::string lowerCase = LowercaseString(upperCase);
657 unsigned RegNum = MatchRegisterName(lowerCase);
658 if (!RegNum) {
659 RegNum = StringSwitch<unsigned>(lowerCase)
660 .Case("r13", ARM::SP)
661 .Case("r14", ARM::LR)
662 .Case("r15", ARM::PC)
663 .Case("ip", ARM::R12)
664 .Default(0);
665 }
666 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000667
Chris Lattnere5658fa2010-10-30 04:09:10 +0000668 Parser.Lex(); // Eat identifier token.
669 return RegNum;
670}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000671
Bill Wendling50d0f582010-11-18 23:43:05 +0000672/// Try to parse a register name. The token must be an Identifier when called.
673/// If it's a register, an AsmOperand is created. Another AsmOperand is created
674/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000675///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000676/// TODO this is likely to change to allow different register types and or to
677/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000678bool ARMAsmParser::
679TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000680 SMLoc S = Parser.getTok().getLoc();
681 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000682 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000683 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000684
Bill Wendling50d0f582010-11-18 23:43:05 +0000685 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000686
Chris Lattnere5658fa2010-10-30 04:09:10 +0000687 const AsmToken &ExclaimTok = Parser.getTok();
688 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000689 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
690 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000691 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000692 }
693
Bill Wendling50d0f582010-11-18 23:43:05 +0000694 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000695}
696
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000697/// MatchCoprocessorOperandName - Try to parse an coprocessor related
698/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
699/// "c5", ...
700static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000701 // Use the same layout as the tablegen'erated register name matcher. Ugly,
702 // but efficient.
703 switch (Name.size()) {
704 default: break;
705 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000706 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000707 return -1;
708 switch (Name[1]) {
709 default: return -1;
710 case '0': return 0;
711 case '1': return 1;
712 case '2': return 2;
713 case '3': return 3;
714 case '4': return 4;
715 case '5': return 5;
716 case '6': return 6;
717 case '7': return 7;
718 case '8': return 8;
719 case '9': return 9;
720 }
721 break;
722 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000723 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000724 return -1;
725 switch (Name[2]) {
726 default: return -1;
727 case '0': return 10;
728 case '1': return 11;
729 case '2': return 12;
730 case '3': return 13;
731 case '4': return 14;
732 case '5': return 15;
733 }
734 break;
735 }
736
737 llvm_unreachable("Unhandled coprocessor operand string!");
738 return -1;
739}
740
Jim Grosbachf922c472011-02-12 01:34:40 +0000741/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000742/// token must be an Identifier when called, and if it is a coprocessor
743/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000744ARMAsmParser::OperandMatchResultTy ARMAsmParser::
745tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000746 SMLoc S = Parser.getTok().getLoc();
747 const AsmToken &Tok = Parser.getTok();
748 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
749
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000750 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000751 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +0000752 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000753
754 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000755 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000756 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000757}
758
Jim Grosbachf922c472011-02-12 01:34:40 +0000759/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000760/// token must be an Identifier when called, and if it is a coprocessor
761/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000762ARMAsmParser::OperandMatchResultTy ARMAsmParser::
763tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000764 SMLoc S = Parser.getTok().getLoc();
765 const AsmToken &Tok = Parser.getTok();
766 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
767
768 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
769 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +0000770 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000771
772 Parser.Lex(); // Eat identifier token.
773 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000774 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000775}
776
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000777/// Parse a register list, return it if successful else return null. The first
778/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000779bool ARMAsmParser::
780ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000781 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000782 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000783 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000784
Bill Wendling7729e062010-11-09 22:44:22 +0000785 // Read the rest of the registers in the list.
786 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000787 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000788
Bill Wendling7729e062010-11-09 22:44:22 +0000789 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000790 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000791 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000792
Sean Callanan18b83232010-01-19 21:44:56 +0000793 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000794 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000795 if (RegTok.isNot(AsmToken::Identifier)) {
796 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000797 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000798 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000799
Bill Wendling1d6a2652010-11-06 10:40:24 +0000800 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000801 if (RegNum == -1) {
802 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000803 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000804 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000805
Bill Wendlinge7176102010-11-06 22:36:58 +0000806 if (IsRange) {
807 int Reg = PrevRegNum;
808 do {
809 ++Reg;
810 Registers.push_back(std::make_pair(Reg, RegLoc));
811 } while (Reg != RegNum);
812 } else {
813 Registers.push_back(std::make_pair(RegNum, RegLoc));
814 }
815
816 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000817 } while (Parser.getTok().is(AsmToken::Comma) ||
818 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000819
820 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000821 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000822 if (RCurlyTok.isNot(AsmToken::RCurly)) {
823 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000824 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000825 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000826
Bill Wendlinge7176102010-11-06 22:36:58 +0000827 SMLoc E = RCurlyTok.getLoc();
828 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000829
Bill Wendlinge7176102010-11-06 22:36:58 +0000830 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000831 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000832 RI = Registers.begin(), RE = Registers.end();
833
Bill Wendling7caebff2011-01-12 21:20:59 +0000834 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000835 bool EmittedWarning = false;
836
Bill Wendling7caebff2011-01-12 21:20:59 +0000837 DenseMap<unsigned, bool> RegMap;
838 RegMap[HighRegNum] = true;
839
Bill Wendlinge7176102010-11-06 22:36:58 +0000840 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000841 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000842 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000843
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000844 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000845 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000846 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000847 }
848
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000849 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000850 Warning(RegInfo.second,
851 "register not in ascending order in register list");
852
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000853 RegMap[Reg] = true;
854 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000855 }
856
Bill Wendling50d0f582010-11-18 23:43:05 +0000857 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
858 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000859}
860
Jim Grosbachf922c472011-02-12 01:34:40 +0000861/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
862ARMAsmParser::OperandMatchResultTy ARMAsmParser::
863tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000864 SMLoc S = Parser.getTok().getLoc();
865 const AsmToken &Tok = Parser.getTok();
866 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
867 StringRef OptStr = Tok.getString();
868
869 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
870 .Case("sy", ARM_MB::SY)
871 .Case("st", ARM_MB::ST)
872 .Case("ish", ARM_MB::ISH)
873 .Case("ishst", ARM_MB::ISHST)
874 .Case("nsh", ARM_MB::NSH)
875 .Case("nshst", ARM_MB::NSHST)
876 .Case("osh", ARM_MB::OSH)
877 .Case("oshst", ARM_MB::OSHST)
878 .Default(~0U);
879
880 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +0000881 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000882
883 Parser.Lex(); // Eat identifier token.
884 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000885 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000886}
887
Bill Wendlinge7176102010-11-06 22:36:58 +0000888/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000889/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000890///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000891/// TODO Only preindexing and postindexing addressing are started, unindexed
892/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000893bool ARMAsmParser::
894ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000895 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000896 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000897 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000898 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000899 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000900
Sean Callanan18b83232010-01-19 21:44:56 +0000901 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000902 if (BaseRegTok.isNot(AsmToken::Identifier)) {
903 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000904 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000905 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000906 int BaseRegNum = TryParseRegister();
907 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000908 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000909 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000910 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000911
Daniel Dunbar05710932011-01-18 05:34:17 +0000912 // The next token must either be a comma or a closing bracket.
913 const AsmToken &Tok = Parser.getTok();
914 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
915 return true;
916
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000917 bool Preindexed = false;
918 bool Postindexed = false;
919 bool OffsetIsReg = false;
920 bool Negative = false;
921 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000922 ARMOperand *WBOp = 0;
923 int OffsetRegNum = -1;
924 bool OffsetRegShifted = false;
925 enum ShiftType ShiftType = Lsl;
926 const MCExpr *ShiftAmount = 0;
927 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000928
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000929 // First look for preindexed address forms, that is after the "[Rn" we now
930 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000931 if (Tok.is(AsmToken::Comma)) {
932 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000933 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000934
Chris Lattner550276e2010-10-28 20:52:15 +0000935 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
936 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000937 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000938 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000939 if (RBracTok.isNot(AsmToken::RBrac)) {
940 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000941 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000942 }
Sean Callanan76264762010-04-02 22:27:05 +0000943 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000944 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000945
Sean Callanan18b83232010-01-19 21:44:56 +0000946 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000947 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000948 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
949 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000950 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000951 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000952 }
Daniel Dunbar05710932011-01-18 05:34:17 +0000953 } else {
954 // The "[Rn" we have so far was not followed by a comma.
955
Jim Grosbach80eb2332010-10-29 17:41:25 +0000956 // If there's anything other than the right brace, this is a post indexing
957 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000958 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000959 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000960
Sean Callanan18b83232010-01-19 21:44:56 +0000961 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000962
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000963 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000964 Postindexed = true;
965 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000966
Chris Lattner550276e2010-10-28 20:52:15 +0000967 if (NextTok.isNot(AsmToken::Comma)) {
968 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000969 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000970 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000971
Sean Callananb9a25b72010-01-19 20:27:46 +0000972 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000973
Chris Lattner550276e2010-10-28 20:52:15 +0000974 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000975 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000976 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000977 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000978 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000979 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000980
981 // Force Offset to exist if used.
982 if (!OffsetIsReg) {
983 if (!Offset)
984 Offset = MCConstantExpr::Create(0, getContext());
985 }
986
987 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
988 OffsetRegNum, OffsetRegShifted,
989 ShiftType, ShiftAmount, Preindexed,
990 Postindexed, Negative, Writeback,
991 S, E));
992 if (WBOp)
993 Operands.push_back(WBOp);
994
995 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000996}
997
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000998/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
999/// we will parse the following (were +/- means that a plus or minus is
1000/// optional):
1001/// +/-Rm
1002/// +/-Rm, shift
1003/// #offset
1004/// we return false on success or an error otherwise.
1005bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001006 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001007 enum ShiftType &ShiftType,
1008 const MCExpr *&ShiftAmount,
1009 const MCExpr *&Offset,
1010 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001011 int &OffsetRegNum,
1012 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001013 Negative = false;
1014 OffsetRegShifted = false;
1015 OffsetIsReg = false;
1016 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001017 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001018 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001019 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001020 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001021 else if (NextTok.is(AsmToken::Minus)) {
1022 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001023 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001024 }
1025 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001026 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001027 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001028 SMLoc CurLoc = OffsetRegTok.getLoc();
1029 OffsetRegNum = TryParseRegister();
1030 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001031 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001032 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001033 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001034 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001035
Bill Wendling12f40e92010-11-06 10:51:53 +00001036 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001037 if (OffsetRegNum != -1) {
1038 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001039 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001040 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001041 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001042
Sean Callanan18b83232010-01-19 21:44:56 +00001043 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001044 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001045 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001046 OffsetRegShifted = true;
1047 }
1048 }
1049 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1050 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001051 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001052 if (HashTok.isNot(AsmToken::Hash))
1053 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001054
Sean Callananb9a25b72010-01-19 20:27:46 +00001055 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001056
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001057 if (getParser().ParseExpression(Offset))
1058 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001059 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001060 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001061 return false;
1062}
1063
1064/// ParseShift as one of these two:
1065/// ( lsl | lsr | asr | ror ) , # shift_amount
1066/// rrx
1067/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +00001068bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +00001069 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001070 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001071 if (Tok.isNot(AsmToken::Identifier))
1072 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001073 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001074 if (ShiftName == "lsl" || ShiftName == "LSL")
1075 St = Lsl;
1076 else if (ShiftName == "lsr" || ShiftName == "LSR")
1077 St = Lsr;
1078 else if (ShiftName == "asr" || ShiftName == "ASR")
1079 St = Asr;
1080 else if (ShiftName == "ror" || ShiftName == "ROR")
1081 St = Ror;
1082 else if (ShiftName == "rrx" || ShiftName == "RRX")
1083 St = Rrx;
1084 else
1085 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001086 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001087
1088 // Rrx stands alone.
1089 if (St == Rrx)
1090 return false;
1091
1092 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001093 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001094 if (HashTok.isNot(AsmToken::Hash))
1095 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001096 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001097
1098 if (getParser().ParseExpression(ShiftAmount))
1099 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001100
1101 return false;
1102}
1103
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001104/// Parse a arm instruction operand. For now this parses the operand regardless
1105/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001106bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001107 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001108 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001109
1110 // Check if the current operand has a custom associated parser, if so, try to
1111 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001112 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1113 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001114 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001115 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1116 // there was a match, but an error occurred, in which case, just return that
1117 // the operand parsing failed.
1118 if (ResTy == MatchOperand_ParseFail)
1119 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001120
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001121 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001122 default:
1123 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001124 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001125 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001126 if (!TryParseRegisterWithWriteBack(Operands))
1127 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001128
1129 // Fall though for the Identifier case that is not a register or a
1130 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001131 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1132 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001133 // This was not a register so parse other operands that start with an
1134 // identifier (like labels) as expressions and create them as immediates.
1135 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001136 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001137 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001138 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001139 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001140 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1141 return false;
1142 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001143 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001144 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001145 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001146 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001147 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001148 // #42 -> immediate.
1149 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001150 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001151 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001152 const MCExpr *ImmVal;
1153 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001154 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001155 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001156 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1157 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001158 case AsmToken::Colon: {
1159 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001160 // FIXME: Check it's an expression prefix,
1161 // e.g. (FOO - :lower16:BAR) isn't legal.
1162 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001163 if (ParsePrefix(RefKind))
1164 return true;
1165
Evan Cheng75972122011-01-13 07:58:56 +00001166 const MCExpr *SubExprVal;
1167 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001168 return true;
1169
Evan Cheng75972122011-01-13 07:58:56 +00001170 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1171 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001172 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001173 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001174 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001175 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001176 }
1177}
1178
Evan Cheng75972122011-01-13 07:58:56 +00001179// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1180// :lower16: and :upper16:.
1181bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1182 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001183
1184 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001185 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001186 Parser.Lex(); // Eat ':'
1187
1188 if (getLexer().isNot(AsmToken::Identifier)) {
1189 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1190 return true;
1191 }
1192
1193 StringRef IDVal = Parser.getTok().getIdentifier();
1194 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001195 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001196 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001197 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001198 } else {
1199 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1200 return true;
1201 }
1202 Parser.Lex();
1203
1204 if (getLexer().isNot(AsmToken::Colon)) {
1205 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1206 return true;
1207 }
1208 Parser.Lex(); // Eat the last ':'
1209 return false;
1210}
1211
1212const MCExpr *
1213ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1214 MCSymbolRefExpr::VariantKind Variant) {
1215 // Recurse over the given expression, rebuilding it to apply the given variant
1216 // to the leftmost symbol.
1217 if (Variant == MCSymbolRefExpr::VK_None)
1218 return E;
1219
1220 switch (E->getKind()) {
1221 case MCExpr::Target:
1222 llvm_unreachable("Can't handle target expr yet");
1223 case MCExpr::Constant:
1224 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1225
1226 case MCExpr::SymbolRef: {
1227 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1228
1229 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1230 return 0;
1231
1232 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1233 }
1234
1235 case MCExpr::Unary:
1236 llvm_unreachable("Can't handle unary expressions yet");
1237
1238 case MCExpr::Binary: {
1239 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1240 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1241 const MCExpr *RHS = BE->getRHS();
1242 if (!LHS)
1243 return 0;
1244
1245 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1246 }
1247 }
1248
1249 assert(0 && "Invalid expression kind!");
1250 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001251}
1252
Daniel Dunbar352e1482011-01-11 15:59:50 +00001253/// \brief Given a mnemonic, split out possible predication code and carry
1254/// setting letters to form a canonical mnemonic and flags.
1255//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001256// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001257static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
1258 unsigned &PredicationCode,
1259 bool &CarrySetting) {
1260 PredicationCode = ARMCC::AL;
1261 CarrySetting = false;
1262
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001263 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001264 //
1265 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001266 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1267 Mnemonic == "movs" ||
1268 Mnemonic == "svc" ||
1269 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1270 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1271 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1272 Mnemonic == "vclt" ||
1273 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1274 Mnemonic == "vcle" ||
1275 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1276 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1277 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001278 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001279
Daniel Dunbar352e1482011-01-11 15:59:50 +00001280 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001281 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001282 .Case("eq", ARMCC::EQ)
1283 .Case("ne", ARMCC::NE)
1284 .Case("hs", ARMCC::HS)
1285 .Case("lo", ARMCC::LO)
1286 .Case("mi", ARMCC::MI)
1287 .Case("pl", ARMCC::PL)
1288 .Case("vs", ARMCC::VS)
1289 .Case("vc", ARMCC::VC)
1290 .Case("hi", ARMCC::HI)
1291 .Case("ls", ARMCC::LS)
1292 .Case("ge", ARMCC::GE)
1293 .Case("lt", ARMCC::LT)
1294 .Case("gt", ARMCC::GT)
1295 .Case("le", ARMCC::LE)
1296 .Case("al", ARMCC::AL)
1297 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001298 if (CC != ~0U) {
1299 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001300 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001301 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001302
Daniel Dunbar352e1482011-01-11 15:59:50 +00001303 // Next, determine if we have a carry setting bit. We explicitly ignore all
1304 // the instructions we know end in 's'.
1305 if (Mnemonic.endswith("s") &&
1306 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1307 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1308 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1309 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1310 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1311 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1312 CarrySetting = true;
1313 }
1314
1315 return Mnemonic;
1316}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001317
1318/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1319/// inclusion of carry set or predication code operands.
1320//
1321// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001322void ARMAsmParser::
1323GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1324 bool &CanAcceptPredicationCode) {
1325 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1326
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001327 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1328 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1329 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1330 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1331 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1332 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1333 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1334 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1335 CanAcceptCarrySet = true;
1336 } else {
1337 CanAcceptCarrySet = false;
1338 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001339
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001340 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1341 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1342 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1343 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001344 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
1345 Mnemonic == "clrex") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001346 CanAcceptPredicationCode = false;
1347 } else {
1348 CanAcceptPredicationCode = true;
1349 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001350
1351 if (isThumb)
1352 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001353 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001354 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001355}
1356
1357/// Parse an arm instruction mnemonic followed by its operands.
1358bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1359 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1360 // Create the leading tokens for the mnemonic, split by '.' characters.
1361 size_t Start = 0, Next = Name.find('.');
1362 StringRef Head = Name.slice(Start, Next);
1363
Daniel Dunbar352e1482011-01-11 15:59:50 +00001364 // Split out the predication code and carry setting flag from the mnemonic.
1365 unsigned PredicationCode;
1366 bool CarrySetting;
1367 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001368
Chris Lattner3a697562010-10-28 17:20:03 +00001369 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001370
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001371 // Next, add the CCOut and ConditionCode operands, if needed.
1372 //
1373 // For mnemonics which can ever incorporate a carry setting bit or predication
1374 // code, our matching model involves us always generating CCOut and
1375 // ConditionCode operands to match the mnemonic "as written" and then we let
1376 // the matcher deal with finding the right instruction or generating an
1377 // appropriate error.
1378 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1379 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1380
1381 // Add the carry setting operand, if necessary.
1382 //
1383 // FIXME: It would be awesome if we could somehow invent a location such that
1384 // match errors on this operand would print a nice diagnostic about how the
1385 // 's' character in the mnemonic resulted in a CCOut operand.
1386 if (CanAcceptCarrySet) {
1387 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1388 NameLoc));
1389 } else {
1390 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1391 // misspelled another mnemonic).
1392
1393 // FIXME: Issue a nice error.
1394 }
1395
1396 // Add the predication code operand, if necessary.
1397 if (CanAcceptPredicationCode) {
1398 Operands.push_back(ARMOperand::CreateCondCode(
1399 ARMCC::CondCodes(PredicationCode), NameLoc));
1400 } else {
1401 // This mnemonic can't ever accept a predication code, but the user wrote
1402 // one (or misspelled another mnemonic).
1403
1404 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001405 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001406
1407 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001408 while (Next != StringRef::npos) {
1409 Start = Next;
1410 Next = Name.find('.', Start + 1);
1411 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001412
Chris Lattner3a697562010-10-28 17:20:03 +00001413 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001414 }
1415
1416 // Read the remaining operands.
1417 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001418 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001419 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001420 Parser.EatToEndOfStatement();
1421 return true;
1422 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001423
1424 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001425 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001426
1427 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001428 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001429 Parser.EatToEndOfStatement();
1430 return true;
1431 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001432 }
1433 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001434
Chris Lattnercbf8a982010-09-11 16:18:25 +00001435 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1436 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001437 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001438 }
Bill Wendling146018f2010-11-06 21:42:12 +00001439
Chris Lattner34e53142010-09-08 05:10:46 +00001440 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001441 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001442}
1443
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001444bool ARMAsmParser::
1445MatchAndEmitInstruction(SMLoc IDLoc,
1446 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1447 MCStreamer &Out) {
1448 MCInst Inst;
1449 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001450 MatchResultTy MatchResult, MatchResult2;
1451 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1452 if (MatchResult != Match_Success) {
1453 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1454 // that does not update the condition codes. So try adding a CCOut operand
1455 // with a value of reg0.
1456 if (MatchResult == Match_InvalidOperand) {
1457 Operands.insert(Operands.begin() + 1,
1458 ARMOperand::CreateCCOut(0,
1459 ((ARMOperand*)Operands[0])->getStartLoc()));
1460 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1461 if (MatchResult2 == Match_Success)
1462 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001463 else {
1464 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001465 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001466 delete CCOut;
1467 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001468 }
1469 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1470 // that updates the condition codes if it ends in 's'. So see if the
1471 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1472 // operand with a value of CPSR.
1473 else if(MatchResult == Match_MnemonicFail) {
1474 // Get the instruction mnemonic, which is the first token.
1475 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1476 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1477 // removed the 's' from the mnemonic for matching.
1478 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1479 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001480 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1481 Operands.erase(Operands.begin());
1482 delete OldMnemonic;
1483 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001484 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1485 Operands.insert(Operands.begin() + 1,
1486 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1487 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1488 if (MatchResult2 == Match_Success)
1489 MatchResult = Match_Success;
1490 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001491 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1492 Operands.erase(Operands.begin());
1493 delete OldMnemonic;
1494 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001495 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001496 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1497 Operands.erase(Operands.begin() + 1);
1498 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001499 }
1500 }
1501 }
1502 }
1503 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001504 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001505 Out.EmitInstruction(Inst);
1506 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001507 case Match_MissingFeature:
1508 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1509 return true;
1510 case Match_InvalidOperand: {
1511 SMLoc ErrorLoc = IDLoc;
1512 if (ErrorInfo != ~0U) {
1513 if (ErrorInfo >= Operands.size())
1514 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001515
Chris Lattnere73d4f82010-10-28 21:41:58 +00001516 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1517 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1518 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001519
Chris Lattnere73d4f82010-10-28 21:41:58 +00001520 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001521 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001522 case Match_MnemonicFail:
1523 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00001524 case Match_ConversionFail:
1525 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00001526 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001527
Eric Christopherc223e2b2010-10-29 09:26:59 +00001528 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001529 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001530}
1531
Kevin Enderby515d5092009-10-15 20:48:48 +00001532/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001533bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1534 StringRef IDVal = DirectiveID.getIdentifier();
1535 if (IDVal == ".word")
1536 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001537 else if (IDVal == ".thumb")
1538 return ParseDirectiveThumb(DirectiveID.getLoc());
1539 else if (IDVal == ".thumb_func")
1540 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1541 else if (IDVal == ".code")
1542 return ParseDirectiveCode(DirectiveID.getLoc());
1543 else if (IDVal == ".syntax")
1544 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001545 return true;
1546}
1547
1548/// ParseDirectiveWord
1549/// ::= .word [ expression (, expression)* ]
1550bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1551 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1552 for (;;) {
1553 const MCExpr *Value;
1554 if (getParser().ParseExpression(Value))
1555 return true;
1556
Chris Lattneraaec2052010-01-19 19:46:13 +00001557 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001558
1559 if (getLexer().is(AsmToken::EndOfStatement))
1560 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001561
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001562 // FIXME: Improve diagnostic.
1563 if (getLexer().isNot(AsmToken::Comma))
1564 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001565 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001566 }
1567 }
1568
Sean Callananb9a25b72010-01-19 20:27:46 +00001569 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001570 return false;
1571}
1572
Kevin Enderby515d5092009-10-15 20:48:48 +00001573/// ParseDirectiveThumb
1574/// ::= .thumb
1575bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1576 if (getLexer().isNot(AsmToken::EndOfStatement))
1577 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001578 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001579
1580 // TODO: set thumb mode
1581 // TODO: tell the MC streamer the mode
1582 // getParser().getStreamer().Emit???();
1583 return false;
1584}
1585
1586/// ParseDirectiveThumbFunc
1587/// ::= .thumbfunc symbol_name
1588bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001589 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001590 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001591 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001592 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001593 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001594 if (getLexer().isNot(AsmToken::EndOfStatement))
1595 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001596 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001597
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001598 // Mark symbol as a thumb symbol.
1599 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1600 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001601 return false;
1602}
1603
1604/// ParseDirectiveSyntax
1605/// ::= .syntax unified | divided
1606bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001607 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001608 if (Tok.isNot(AsmToken::Identifier))
1609 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001610 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001611 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001612 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001613 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00001614 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00001615 else
1616 return Error(L, "unrecognized syntax mode in .syntax directive");
1617
1618 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001619 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001620 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001621
1622 // TODO tell the MC streamer the mode
1623 // getParser().getStreamer().Emit???();
1624 return false;
1625}
1626
1627/// ParseDirectiveCode
1628/// ::= .code 16 | 32
1629bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001630 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001631 if (Tok.isNot(AsmToken::Integer))
1632 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001633 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001634 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001635 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001636 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001637 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001638 else
1639 return Error(L, "invalid operand to .code directive");
1640
1641 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001642 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001643 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001644
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001645 // FIXME: We need to be able switch subtargets at this point so that
1646 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1647 // includes Feature_IsThumb or not to match the right instructions. This is
1648 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1649 if (Val == 16){
1650 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1651 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001652 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001653 }
1654 else{
1655 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1656 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001657 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001658 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001659
Kevin Enderby515d5092009-10-15 20:48:48 +00001660 return false;
1661}
1662
Sean Callanan90b70972010-04-07 20:29:34 +00001663extern "C" void LLVMInitializeARMAsmLexer();
1664
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001665/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001666extern "C" void LLVMInitializeARMAsmParser() {
1667 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1668 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001669 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001670}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001671
Chris Lattner0692ee62010-09-06 19:11:01 +00001672#define GET_REGISTER_MATCHER
1673#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001674#include "ARMGenAsmMatcher.inc"