blob: 3280e7792af47eb61fc23c53c3d705927565fff0 [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*> &);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000058 bool ParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*>&);
59 bool ParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*>&);
Bill Wendling50d0f582010-11-18 23:43:05 +000060 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
61 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000062 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000063 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000064 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
65 MCSymbolRefExpr::VariantKind Variant);
66
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000067
Kevin Enderby9c41fa82009-10-30 22:55:57 +000068 bool ParseMemoryOffsetReg(bool &Negative,
69 bool &OffsetRegShifted,
70 enum ShiftType &ShiftType,
71 const MCExpr *&ShiftAmount,
72 const MCExpr *&Offset,
73 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000074 int &OffsetRegNum,
75 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000076 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000077 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000078 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000080 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000081 bool ParseDirectiveSyntax(SMLoc L);
82
Chris Lattner7036f8b2010-09-29 01:42:58 +000083 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000084 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000085 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000086 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
87 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000088
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000089 /// @name Auto-generated Match Functions
90 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000091
Chris Lattner0692ee62010-09-06 19:11:01 +000092#define GET_ASSEMBLER_HEADER
93#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000094
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000095 /// }
96
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000097public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000098 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000099 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
100 // Initialize the set of available features.
101 setAvailableFeatures(ComputeAvailableFeatures(
102 &TM.getSubtarget<ARMSubtarget>()));
103 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000104
Benjamin Kramer38e59892010-07-14 22:38:02 +0000105 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000106 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000107 virtual bool ParseDirective(AsmToken DirectiveID);
108};
Jim Grosbach16c74252010-10-29 14:46:02 +0000109} // end anonymous namespace
110
Chris Lattner3a697562010-10-28 17:20:03 +0000111namespace {
112
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000113/// ARMOperand - Instances of this class represent a parsed ARM machine
114/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000115class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000116 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000117 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000118 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000119 CoprocNum,
120 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000121 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000122 Memory,
123 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000124 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000125 DPRRegisterList,
126 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000127 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000128 } Kind;
129
Sean Callanan76264762010-04-02 22:27:05 +0000130 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000131 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000132
133 union {
134 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000135 ARMCC::CondCodes Val;
136 } CC;
137
138 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000139 unsigned Val;
140 } Cop;
141
142 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000143 const char *Data;
144 unsigned Length;
145 } Tok;
146
147 struct {
148 unsigned RegNum;
149 } Reg;
150
Bill Wendling8155e5b2010-11-06 22:19:43 +0000151 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000152 const MCExpr *Val;
153 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000154
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000155 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000156 struct {
157 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000158 union {
159 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
160 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
161 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000162 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
163 enum ShiftType ShiftType; // used when OffsetRegShifted is true
164 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000165 unsigned Preindexed : 1;
166 unsigned Postindexed : 1;
167 unsigned OffsetIsReg : 1;
168 unsigned Negative : 1; // only used when OffsetIsReg is true
169 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000170 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000171 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000172
Bill Wendling146018f2010-11-06 21:42:12 +0000173 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
174public:
Sean Callanan76264762010-04-02 22:27:05 +0000175 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
176 Kind = o.Kind;
177 StartLoc = o.StartLoc;
178 EndLoc = o.EndLoc;
179 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000180 case CondCode:
181 CC = o.CC;
182 break;
Sean Callanan76264762010-04-02 22:27:05 +0000183 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000184 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000185 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000186 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000187 case Register:
188 Reg = o.Reg;
189 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000190 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000191 case DPRRegisterList:
192 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000193 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000194 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000195 case CoprocNum:
196 case CoprocReg:
197 Cop = o.Cop;
198 break;
Sean Callanan76264762010-04-02 22:27:05 +0000199 case Immediate:
200 Imm = o.Imm;
201 break;
202 case Memory:
203 Mem = o.Mem;
204 break;
205 }
206 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000207
Sean Callanan76264762010-04-02 22:27:05 +0000208 /// getStartLoc - Get the location of the first token of this operand.
209 SMLoc getStartLoc() const { return StartLoc; }
210 /// getEndLoc - Get the location of the last token of this operand.
211 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000212
Daniel Dunbar8462b302010-08-11 06:36:53 +0000213 ARMCC::CondCodes getCondCode() const {
214 assert(Kind == CondCode && "Invalid access!");
215 return CC.Val;
216 }
217
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000218 unsigned getCoproc() const {
219 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
220 return Cop.Val;
221 }
222
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000223 StringRef getToken() const {
224 assert(Kind == Token && "Invalid access!");
225 return StringRef(Tok.Data, Tok.Length);
226 }
227
228 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000229 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000230 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000231 }
232
Bill Wendling5fa22a12010-11-09 23:28:44 +0000233 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000234 assert((Kind == RegisterList || Kind == DPRRegisterList ||
235 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000236 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000237 }
238
Kevin Enderbycfe07242009-10-13 22:19:02 +0000239 const MCExpr *getImm() const {
240 assert(Kind == Immediate && "Invalid access!");
241 return Imm.Val;
242 }
243
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000244 /// @name Memory Operand Accessors
245 /// @{
246
247 unsigned getMemBaseRegNum() const {
248 return Mem.BaseRegNum;
249 }
250 unsigned getMemOffsetRegNum() const {
251 assert(Mem.OffsetIsReg && "Invalid access!");
252 return Mem.Offset.RegNum;
253 }
254 const MCExpr *getMemOffset() const {
255 assert(!Mem.OffsetIsReg && "Invalid access!");
256 return Mem.Offset.Value;
257 }
258 unsigned getMemOffsetRegShifted() const {
259 assert(Mem.OffsetIsReg && "Invalid access!");
260 return Mem.OffsetRegShifted;
261 }
262 const MCExpr *getMemShiftAmount() const {
263 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
264 return Mem.ShiftAmount;
265 }
266 enum ShiftType getMemShiftType() const {
267 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
268 return Mem.ShiftType;
269 }
270 bool getMemPreindexed() const { return Mem.Preindexed; }
271 bool getMemPostindexed() const { return Mem.Postindexed; }
272 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
273 bool getMemNegative() const { return Mem.Negative; }
274 bool getMemWriteback() const { return Mem.Writeback; }
275
276 /// @}
277
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000278 bool isCoprocNum() const { return Kind == CoprocNum; }
279 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000280 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000281 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000282 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000283 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000284 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000285 bool isDPRRegList() const { return Kind == DPRRegisterList; }
286 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000287 bool isToken() const { return Kind == Token; }
288 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000289 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000290 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
291 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000292 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000293
Daniel Dunbar4b462672011-01-18 05:55:27 +0000294 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000295 if (!CE) return false;
296
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000297 // The offset must be a multiple of 4 in the range 0-1020.
298 int64_t Value = CE->getValue();
299 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
300 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000301 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000302 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000303 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000304 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000305 }
306 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000307 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000308 return false;
309
Daniel Dunbar4b462672011-01-18 05:55:27 +0000310 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000311 if (!CE) return false;
312
313 // The offset must be a multiple of 4 in the range 0-124.
314 uint64_t Value = CE->getValue();
315 return ((Value & 0x3) == 0 && Value <= 124);
316 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000317
318 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000319 // Add as immediates when possible. Null MCExpr = 0.
320 if (Expr == 0)
321 Inst.addOperand(MCOperand::CreateImm(0));
322 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000323 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
324 else
325 Inst.addOperand(MCOperand::CreateExpr(Expr));
326 }
327
Daniel Dunbar8462b302010-08-11 06:36:53 +0000328 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000329 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000330 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000331 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
332 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000333 }
334
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000335 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
336 assert(N == 1 && "Invalid number of operands!");
337 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
338 }
339
340 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
341 assert(N == 1 && "Invalid number of operands!");
342 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
343 }
344
Jim Grosbachd67641b2010-12-06 18:21:12 +0000345 void addCCOutOperands(MCInst &Inst, unsigned N) const {
346 assert(N == 1 && "Invalid number of operands!");
347 Inst.addOperand(MCOperand::CreateReg(getReg()));
348 }
349
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000350 void addRegOperands(MCInst &Inst, unsigned N) const {
351 assert(N == 1 && "Invalid number of operands!");
352 Inst.addOperand(MCOperand::CreateReg(getReg()));
353 }
354
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000355 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000356 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000357 const SmallVectorImpl<unsigned> &RegList = getRegList();
358 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000359 I = RegList.begin(), E = RegList.end(); I != E; ++I)
360 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000361 }
362
Bill Wendling0f630752010-11-17 04:32:08 +0000363 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
364 addRegListOperands(Inst, N);
365 }
366
367 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
368 addRegListOperands(Inst, N);
369 }
370
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000371 void addImmOperands(MCInst &Inst, unsigned N) const {
372 assert(N == 1 && "Invalid number of operands!");
373 addExpr(Inst, getImm());
374 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000375
Chris Lattner14b93852010-10-29 00:27:31 +0000376 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
377 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000378
Daniel Dunbar4b462672011-01-18 05:55:27 +0000379 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
380 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000381
Jim Grosbach80eb2332010-10-29 17:41:25 +0000382 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
383 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000384 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000385 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000386
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000387 // The MCInst offset operand doesn't include the low two bits (like
388 // the instruction encoding).
389 int64_t Offset = CE->getValue() / 4;
390 if (Offset >= 0)
391 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
392 Offset)));
393 else
394 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
395 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000396 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000397
Bill Wendlingf4caf692010-12-14 03:36:38 +0000398 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
399 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000400 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
401 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000402 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000403
Bill Wendlingf4caf692010-12-14 03:36:38 +0000404 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
405 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000406 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
407 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000408 assert(CE && "Non-constant mode offset operand!");
409 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000410 }
411
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000412 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000413
Chris Lattner3a697562010-10-28 17:20:03 +0000414 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
415 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000416 Op->CC.Val = CC;
417 Op->StartLoc = S;
418 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000419 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000420 }
421
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000422 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
423 ARMOperand *Op = new ARMOperand(CoprocNum);
424 Op->Cop.Val = CopVal;
425 Op->StartLoc = S;
426 Op->EndLoc = S;
427 return Op;
428 }
429
430 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
431 ARMOperand *Op = new ARMOperand(CoprocReg);
432 Op->Cop.Val = CopVal;
433 Op->StartLoc = S;
434 Op->EndLoc = S;
435 return Op;
436 }
437
Jim Grosbachd67641b2010-12-06 18:21:12 +0000438 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
439 ARMOperand *Op = new ARMOperand(CCOut);
440 Op->Reg.RegNum = RegNum;
441 Op->StartLoc = S;
442 Op->EndLoc = S;
443 return Op;
444 }
445
Chris Lattner3a697562010-10-28 17:20:03 +0000446 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
447 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000448 Op->Tok.Data = Str.data();
449 Op->Tok.Length = Str.size();
450 Op->StartLoc = S;
451 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000452 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000453 }
454
Bill Wendling50d0f582010-11-18 23:43:05 +0000455 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000456 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000457 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000458 Op->StartLoc = S;
459 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000460 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000461 }
462
Bill Wendling7729e062010-11-09 22:44:22 +0000463 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000464 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000465 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000466 KindTy Kind = RegisterList;
467
468 if (ARM::DPRRegClass.contains(Regs.front().first))
469 Kind = DPRRegisterList;
470 else if (ARM::SPRRegClass.contains(Regs.front().first))
471 Kind = SPRRegisterList;
472
473 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000474 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000475 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000476 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000477 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000478 Op->StartLoc = StartLoc;
479 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000480 return Op;
481 }
482
Chris Lattner3a697562010-10-28 17:20:03 +0000483 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
484 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000485 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000486 Op->StartLoc = S;
487 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000488 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000489 }
490
Chris Lattner3a697562010-10-28 17:20:03 +0000491 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000492 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000493 bool OffsetRegShifted, enum ShiftType ShiftType,
494 const MCExpr *ShiftAmount, bool Preindexed,
495 bool Postindexed, bool Negative, bool Writeback,
496 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000497 assert((OffsetRegNum == -1 || OffsetIsReg) &&
498 "OffsetRegNum must imply OffsetIsReg!");
499 assert((!OffsetRegShifted || OffsetIsReg) &&
500 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000501 assert((Offset || OffsetIsReg) &&
502 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000503 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
504 "Cannot have shift amount without shifted register offset!");
505 assert((!Offset || !OffsetIsReg) &&
506 "Cannot have expression offset and register offset!");
507
Chris Lattner3a697562010-10-28 17:20:03 +0000508 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000509 Op->Mem.BaseRegNum = BaseRegNum;
510 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000511 if (OffsetIsReg)
512 Op->Mem.Offset.RegNum = OffsetRegNum;
513 else
514 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000515 Op->Mem.OffsetRegShifted = OffsetRegShifted;
516 Op->Mem.ShiftType = ShiftType;
517 Op->Mem.ShiftAmount = ShiftAmount;
518 Op->Mem.Preindexed = Preindexed;
519 Op->Mem.Postindexed = Postindexed;
520 Op->Mem.Negative = Negative;
521 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000522
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};
528
529} // end anonymous namespace.
530
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000531void ARMOperand::dump(raw_ostream &OS) const {
532 switch (Kind) {
533 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000534 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000535 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000536 case CCOut:
537 OS << "<ccout " << getReg() << ">";
538 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000539 case CoprocNum:
540 OS << "<coprocessor number: " << getCoproc() << ">";
541 break;
542 case CoprocReg:
543 OS << "<coprocessor register: " << getCoproc() << ">";
544 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000545 case Immediate:
546 getImm()->print(OS);
547 break;
548 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000549 OS << "<memory "
550 << "base:" << getMemBaseRegNum();
551 if (getMemOffsetIsReg()) {
552 OS << " offset:<register " << getMemOffsetRegNum();
553 if (getMemOffsetRegShifted()) {
554 OS << " offset-shift-type:" << getMemShiftType();
555 OS << " offset-shift-amount:" << *getMemShiftAmount();
556 }
557 } else {
558 OS << " offset:" << *getMemOffset();
559 }
560 if (getMemOffsetIsReg())
561 OS << " (offset-is-reg)";
562 if (getMemPreindexed())
563 OS << " (pre-indexed)";
564 if (getMemPostindexed())
565 OS << " (post-indexed)";
566 if (getMemNegative())
567 OS << " (negative)";
568 if (getMemWriteback())
569 OS << " (writeback)";
570 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000571 break;
572 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000573 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000574 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000575 case RegisterList:
576 case DPRRegisterList:
577 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000578 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000579
Bill Wendling5fa22a12010-11-09 23:28:44 +0000580 const SmallVectorImpl<unsigned> &RegList = getRegList();
581 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000582 I = RegList.begin(), E = RegList.end(); I != E; ) {
583 OS << *I;
584 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000585 }
586
587 OS << ">";
588 break;
589 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000590 case Token:
591 OS << "'" << getToken() << "'";
592 break;
593 }
594}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000595
596/// @name Auto-generated Match Functions
597/// {
598
599static unsigned MatchRegisterName(StringRef Name);
600
601/// }
602
Bob Wilson69df7232011-02-03 21:46:10 +0000603bool ARMAsmParser::ParseRegister(unsigned &RegNo,
604 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000605 RegNo = TryParseRegister();
606
607 return (RegNo == (unsigned)-1);
608}
609
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000610/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000611/// and if it is a register name the token is eaten and the register number is
612/// returned. Otherwise return -1.
613///
614int ARMAsmParser::TryParseRegister() {
615 const AsmToken &Tok = Parser.getTok();
616 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000617
Chris Lattnere5658fa2010-10-30 04:09:10 +0000618 // FIXME: Validate register for the current architecture; we have to do
619 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000620 std::string upperCase = Tok.getString().str();
621 std::string lowerCase = LowercaseString(upperCase);
622 unsigned RegNum = MatchRegisterName(lowerCase);
623 if (!RegNum) {
624 RegNum = StringSwitch<unsigned>(lowerCase)
625 .Case("r13", ARM::SP)
626 .Case("r14", ARM::LR)
627 .Case("r15", ARM::PC)
628 .Case("ip", ARM::R12)
629 .Default(0);
630 }
631 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000632
Chris Lattnere5658fa2010-10-30 04:09:10 +0000633 Parser.Lex(); // Eat identifier token.
634 return RegNum;
635}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000636
Bill Wendling50d0f582010-11-18 23:43:05 +0000637/// Try to parse a register name. The token must be an Identifier when called.
638/// If it's a register, an AsmOperand is created. Another AsmOperand is created
639/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000640///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000641/// TODO this is likely to change to allow different register types and or to
642/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000643bool ARMAsmParser::
644TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000645 SMLoc S = Parser.getTok().getLoc();
646 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000647 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000648 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000649
Bill Wendling50d0f582010-11-18 23:43:05 +0000650 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000651
Chris Lattnere5658fa2010-10-30 04:09:10 +0000652 const AsmToken &ExclaimTok = Parser.getTok();
653 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000654 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
655 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000656 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000657 }
658
Bill Wendling50d0f582010-11-18 23:43:05 +0000659 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000660}
661
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000662/// MatchCoprocessorOperandName - Try to parse an coprocessor related
663/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
664/// "c5", ...
665static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000666 // Use the same layout as the tablegen'erated register name matcher. Ugly,
667 // but efficient.
668 switch (Name.size()) {
669 default: break;
670 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000671 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000672 return -1;
673 switch (Name[1]) {
674 default: return -1;
675 case '0': return 0;
676 case '1': return 1;
677 case '2': return 2;
678 case '3': return 3;
679 case '4': return 4;
680 case '5': return 5;
681 case '6': return 6;
682 case '7': return 7;
683 case '8': return 8;
684 case '9': return 9;
685 }
686 break;
687 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000688 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000689 return -1;
690 switch (Name[2]) {
691 default: return -1;
692 case '0': return 10;
693 case '1': return 11;
694 case '2': return 12;
695 case '3': return 13;
696 case '4': return 14;
697 case '5': return 15;
698 }
699 break;
700 }
701
702 llvm_unreachable("Unhandled coprocessor operand string!");
703 return -1;
704}
705
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000706/// ParseCoprocNumOperand - Try to parse an coprocessor number operand. The
707/// token must be an Identifier when called, and if it is a coprocessor
708/// number, the token is eaten and the operand is added to the operand list.
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000709bool ARMAsmParser::
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000710ParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000711 SMLoc S = Parser.getTok().getLoc();
712 const AsmToken &Tok = Parser.getTok();
713 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
714
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000715 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000716 if (Num == -1)
717 return true;
718
719 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000720 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
721 return false;
722}
723
724/// ParseCoprocRegOperand - Try to parse an coprocessor register operand. The
725/// token must be an Identifier when called, and if it is a coprocessor
726/// number, the token is eaten and the operand is added to the operand list.
727bool ARMAsmParser::
728ParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
729 SMLoc S = Parser.getTok().getLoc();
730 const AsmToken &Tok = Parser.getTok();
731 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
732
733 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
734 if (Reg == -1)
735 return true;
736
737 Parser.Lex(); // Eat identifier token.
738 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000739 return false;
740}
741
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000742/// Parse a register list, return it if successful else return null. The first
743/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000744bool ARMAsmParser::
745ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000746 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000747 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000748 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000749
Bill Wendling7729e062010-11-09 22:44:22 +0000750 // Read the rest of the registers in the list.
751 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000752 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000753
Bill Wendling7729e062010-11-09 22:44:22 +0000754 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000755 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000756 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000757
Sean Callanan18b83232010-01-19 21:44:56 +0000758 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000759 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000760 if (RegTok.isNot(AsmToken::Identifier)) {
761 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000762 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000763 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000764
Bill Wendling1d6a2652010-11-06 10:40:24 +0000765 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000766 if (RegNum == -1) {
767 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000768 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000769 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000770
Bill Wendlinge7176102010-11-06 22:36:58 +0000771 if (IsRange) {
772 int Reg = PrevRegNum;
773 do {
774 ++Reg;
775 Registers.push_back(std::make_pair(Reg, RegLoc));
776 } while (Reg != RegNum);
777 } else {
778 Registers.push_back(std::make_pair(RegNum, RegLoc));
779 }
780
781 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000782 } while (Parser.getTok().is(AsmToken::Comma) ||
783 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000784
785 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000786 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000787 if (RCurlyTok.isNot(AsmToken::RCurly)) {
788 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000789 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000790 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000791
Bill Wendlinge7176102010-11-06 22:36:58 +0000792 SMLoc E = RCurlyTok.getLoc();
793 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000794
Bill Wendlinge7176102010-11-06 22:36:58 +0000795 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000796 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000797 RI = Registers.begin(), RE = Registers.end();
798
Bill Wendling7caebff2011-01-12 21:20:59 +0000799 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000800 bool EmittedWarning = false;
801
Bill Wendling7caebff2011-01-12 21:20:59 +0000802 DenseMap<unsigned, bool> RegMap;
803 RegMap[HighRegNum] = true;
804
Bill Wendlinge7176102010-11-06 22:36:58 +0000805 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000806 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000807 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000808
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000809 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000810 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000811 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000812 }
813
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000814 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000815 Warning(RegInfo.second,
816 "register not in ascending order in register list");
817
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000818 RegMap[Reg] = true;
819 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000820 }
821
Bill Wendling50d0f582010-11-18 23:43:05 +0000822 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
823 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000824}
825
Bill Wendlinge7176102010-11-06 22:36:58 +0000826/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000827/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000828///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000829/// TODO Only preindexing and postindexing addressing are started, unindexed
830/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000831bool ARMAsmParser::
832ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000833 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000834 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000835 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000836 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000837 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000838
Sean Callanan18b83232010-01-19 21:44:56 +0000839 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000840 if (BaseRegTok.isNot(AsmToken::Identifier)) {
841 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000842 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000843 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000844 int BaseRegNum = TryParseRegister();
845 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000846 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000847 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000848 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000849
Daniel Dunbar05710932011-01-18 05:34:17 +0000850 // The next token must either be a comma or a closing bracket.
851 const AsmToken &Tok = Parser.getTok();
852 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
853 return true;
854
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000855 bool Preindexed = false;
856 bool Postindexed = false;
857 bool OffsetIsReg = false;
858 bool Negative = false;
859 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000860 ARMOperand *WBOp = 0;
861 int OffsetRegNum = -1;
862 bool OffsetRegShifted = false;
863 enum ShiftType ShiftType = Lsl;
864 const MCExpr *ShiftAmount = 0;
865 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000866
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000867 // First look for preindexed address forms, that is after the "[Rn" we now
868 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000869 if (Tok.is(AsmToken::Comma)) {
870 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000871 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000872
Chris Lattner550276e2010-10-28 20:52:15 +0000873 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
874 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000875 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000876 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000877 if (RBracTok.isNot(AsmToken::RBrac)) {
878 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000879 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000880 }
Sean Callanan76264762010-04-02 22:27:05 +0000881 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000882 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000883
Sean Callanan18b83232010-01-19 21:44:56 +0000884 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000885 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000886 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
887 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000888 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000889 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000890 }
Daniel Dunbar05710932011-01-18 05:34:17 +0000891 } else {
892 // The "[Rn" we have so far was not followed by a comma.
893
Jim Grosbach80eb2332010-10-29 17:41:25 +0000894 // If there's anything other than the right brace, this is a post indexing
895 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000896 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000897 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000898
Sean Callanan18b83232010-01-19 21:44:56 +0000899 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000900
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000901 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000902 Postindexed = true;
903 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000904
Chris Lattner550276e2010-10-28 20:52:15 +0000905 if (NextTok.isNot(AsmToken::Comma)) {
906 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000907 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000908 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000909
Sean Callananb9a25b72010-01-19 20:27:46 +0000910 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000911
Chris Lattner550276e2010-10-28 20:52:15 +0000912 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000913 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000914 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000915 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000916 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000917 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000918
919 // Force Offset to exist if used.
920 if (!OffsetIsReg) {
921 if (!Offset)
922 Offset = MCConstantExpr::Create(0, getContext());
923 }
924
925 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
926 OffsetRegNum, OffsetRegShifted,
927 ShiftType, ShiftAmount, Preindexed,
928 Postindexed, Negative, Writeback,
929 S, E));
930 if (WBOp)
931 Operands.push_back(WBOp);
932
933 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000934}
935
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000936/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
937/// we will parse the following (were +/- means that a plus or minus is
938/// optional):
939/// +/-Rm
940/// +/-Rm, shift
941/// #offset
942/// we return false on success or an error otherwise.
943bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000944 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000945 enum ShiftType &ShiftType,
946 const MCExpr *&ShiftAmount,
947 const MCExpr *&Offset,
948 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000949 int &OffsetRegNum,
950 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000951 Negative = false;
952 OffsetRegShifted = false;
953 OffsetIsReg = false;
954 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000955 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000956 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000957 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000958 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000959 else if (NextTok.is(AsmToken::Minus)) {
960 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000961 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000962 }
963 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000964 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000965 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000966 SMLoc CurLoc = OffsetRegTok.getLoc();
967 OffsetRegNum = TryParseRegister();
968 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000969 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000970 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000971 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000972 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000973
Bill Wendling12f40e92010-11-06 10:51:53 +0000974 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000975 if (OffsetRegNum != -1) {
976 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000977 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000978 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000979 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000980
Sean Callanan18b83232010-01-19 21:44:56 +0000981 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000982 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000983 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000984 OffsetRegShifted = true;
985 }
986 }
987 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
988 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000989 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000990 if (HashTok.isNot(AsmToken::Hash))
991 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000992
Sean Callananb9a25b72010-01-19 20:27:46 +0000993 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000994
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000995 if (getParser().ParseExpression(Offset))
996 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000997 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000998 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000999 return false;
1000}
1001
1002/// ParseShift as one of these two:
1003/// ( lsl | lsr | asr | ror ) , # shift_amount
1004/// rrx
1005/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +00001006bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +00001007 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001008 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001009 if (Tok.isNot(AsmToken::Identifier))
1010 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001011 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001012 if (ShiftName == "lsl" || ShiftName == "LSL")
1013 St = Lsl;
1014 else if (ShiftName == "lsr" || ShiftName == "LSR")
1015 St = Lsr;
1016 else if (ShiftName == "asr" || ShiftName == "ASR")
1017 St = Asr;
1018 else if (ShiftName == "ror" || ShiftName == "ROR")
1019 St = Ror;
1020 else if (ShiftName == "rrx" || ShiftName == "RRX")
1021 St = Rrx;
1022 else
1023 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001024 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001025
1026 // Rrx stands alone.
1027 if (St == Rrx)
1028 return false;
1029
1030 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001031 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001032 if (HashTok.isNot(AsmToken::Hash))
1033 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001034 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001035
1036 if (getParser().ParseExpression(ShiftAmount))
1037 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001038
1039 return false;
1040}
1041
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001042/// Parse a arm instruction operand. For now this parses the operand regardless
1043/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001044bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001045 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001046 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001047
1048 // Check if the current operand has a custom associated parser, if so, try to
1049 // custom parse the operand, or fallback to the general approach.
1050 MatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1051 if (ResTy == Match_Success)
1052 return false;
1053
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001054 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001055 default:
1056 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001057 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001058 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001059 if (!TryParseRegisterWithWriteBack(Operands))
1060 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001061
1062 // Fall though for the Identifier case that is not a register or a
1063 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001064 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1065 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001066 // This was not a register so parse other operands that start with an
1067 // identifier (like labels) as expressions and create them as immediates.
1068 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001069 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001070 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001071 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001072 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001073 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1074 return false;
1075 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001076 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001077 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001078 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001079 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001080 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001081 // #42 -> immediate.
1082 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001083 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001084 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001085 const MCExpr *ImmVal;
1086 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001087 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001088 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001089 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1090 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001091 case AsmToken::Colon: {
1092 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001093 // FIXME: Check it's an expression prefix,
1094 // e.g. (FOO - :lower16:BAR) isn't legal.
1095 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001096 if (ParsePrefix(RefKind))
1097 return true;
1098
Evan Cheng75972122011-01-13 07:58:56 +00001099 const MCExpr *SubExprVal;
1100 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001101 return true;
1102
Evan Cheng75972122011-01-13 07:58:56 +00001103 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1104 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001105 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001106 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001107 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001108 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001109 }
1110}
1111
Evan Cheng75972122011-01-13 07:58:56 +00001112// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1113// :lower16: and :upper16:.
1114bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1115 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001116
1117 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001118 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001119 Parser.Lex(); // Eat ':'
1120
1121 if (getLexer().isNot(AsmToken::Identifier)) {
1122 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1123 return true;
1124 }
1125
1126 StringRef IDVal = Parser.getTok().getIdentifier();
1127 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001128 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001129 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001130 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001131 } else {
1132 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1133 return true;
1134 }
1135 Parser.Lex();
1136
1137 if (getLexer().isNot(AsmToken::Colon)) {
1138 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1139 return true;
1140 }
1141 Parser.Lex(); // Eat the last ':'
1142 return false;
1143}
1144
1145const MCExpr *
1146ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1147 MCSymbolRefExpr::VariantKind Variant) {
1148 // Recurse over the given expression, rebuilding it to apply the given variant
1149 // to the leftmost symbol.
1150 if (Variant == MCSymbolRefExpr::VK_None)
1151 return E;
1152
1153 switch (E->getKind()) {
1154 case MCExpr::Target:
1155 llvm_unreachable("Can't handle target expr yet");
1156 case MCExpr::Constant:
1157 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1158
1159 case MCExpr::SymbolRef: {
1160 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1161
1162 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1163 return 0;
1164
1165 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1166 }
1167
1168 case MCExpr::Unary:
1169 llvm_unreachable("Can't handle unary expressions yet");
1170
1171 case MCExpr::Binary: {
1172 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1173 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1174 const MCExpr *RHS = BE->getRHS();
1175 if (!LHS)
1176 return 0;
1177
1178 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1179 }
1180 }
1181
1182 assert(0 && "Invalid expression kind!");
1183 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001184}
1185
Daniel Dunbar352e1482011-01-11 15:59:50 +00001186/// \brief Given a mnemonic, split out possible predication code and carry
1187/// setting letters to form a canonical mnemonic and flags.
1188//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001189// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001190static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
1191 unsigned &PredicationCode,
1192 bool &CarrySetting) {
1193 PredicationCode = ARMCC::AL;
1194 CarrySetting = false;
1195
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001196 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001197 //
1198 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001199 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1200 Mnemonic == "movs" ||
1201 Mnemonic == "svc" ||
1202 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1203 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1204 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1205 Mnemonic == "vclt" ||
1206 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1207 Mnemonic == "vcle" ||
1208 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1209 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1210 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001211 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001212
Daniel Dunbar352e1482011-01-11 15:59:50 +00001213 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001214 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001215 .Case("eq", ARMCC::EQ)
1216 .Case("ne", ARMCC::NE)
1217 .Case("hs", ARMCC::HS)
1218 .Case("lo", ARMCC::LO)
1219 .Case("mi", ARMCC::MI)
1220 .Case("pl", ARMCC::PL)
1221 .Case("vs", ARMCC::VS)
1222 .Case("vc", ARMCC::VC)
1223 .Case("hi", ARMCC::HI)
1224 .Case("ls", ARMCC::LS)
1225 .Case("ge", ARMCC::GE)
1226 .Case("lt", ARMCC::LT)
1227 .Case("gt", ARMCC::GT)
1228 .Case("le", ARMCC::LE)
1229 .Case("al", ARMCC::AL)
1230 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001231 if (CC != ~0U) {
1232 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001233 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001234 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001235
Daniel Dunbar352e1482011-01-11 15:59:50 +00001236 // Next, determine if we have a carry setting bit. We explicitly ignore all
1237 // the instructions we know end in 's'.
1238 if (Mnemonic.endswith("s") &&
1239 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1240 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1241 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1242 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1243 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1244 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1245 CarrySetting = true;
1246 }
1247
1248 return Mnemonic;
1249}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001250
1251/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1252/// inclusion of carry set or predication code operands.
1253//
1254// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001255void ARMAsmParser::
1256GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1257 bool &CanAcceptPredicationCode) {
1258 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1259
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001260 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1261 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1262 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1263 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1264 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1265 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1266 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1267 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1268 CanAcceptCarrySet = true;
1269 } else {
1270 CanAcceptCarrySet = false;
1271 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001272
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001273 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1274 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1275 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1276 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001277 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
1278 Mnemonic == "clrex") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001279 CanAcceptPredicationCode = false;
1280 } else {
1281 CanAcceptPredicationCode = true;
1282 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001283
1284 if (isThumb)
1285 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001286 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001287 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001288}
1289
1290/// Parse an arm instruction mnemonic followed by its operands.
1291bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1292 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1293 // Create the leading tokens for the mnemonic, split by '.' characters.
1294 size_t Start = 0, Next = Name.find('.');
1295 StringRef Head = Name.slice(Start, Next);
1296
Daniel Dunbar352e1482011-01-11 15:59:50 +00001297 // Split out the predication code and carry setting flag from the mnemonic.
1298 unsigned PredicationCode;
1299 bool CarrySetting;
1300 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001301
Chris Lattner3a697562010-10-28 17:20:03 +00001302 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001303
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001304 // Next, add the CCOut and ConditionCode operands, if needed.
1305 //
1306 // For mnemonics which can ever incorporate a carry setting bit or predication
1307 // code, our matching model involves us always generating CCOut and
1308 // ConditionCode operands to match the mnemonic "as written" and then we let
1309 // the matcher deal with finding the right instruction or generating an
1310 // appropriate error.
1311 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1312 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1313
1314 // Add the carry setting operand, if necessary.
1315 //
1316 // FIXME: It would be awesome if we could somehow invent a location such that
1317 // match errors on this operand would print a nice diagnostic about how the
1318 // 's' character in the mnemonic resulted in a CCOut operand.
1319 if (CanAcceptCarrySet) {
1320 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1321 NameLoc));
1322 } else {
1323 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1324 // misspelled another mnemonic).
1325
1326 // FIXME: Issue a nice error.
1327 }
1328
1329 // Add the predication code operand, if necessary.
1330 if (CanAcceptPredicationCode) {
1331 Operands.push_back(ARMOperand::CreateCondCode(
1332 ARMCC::CondCodes(PredicationCode), NameLoc));
1333 } else {
1334 // This mnemonic can't ever accept a predication code, but the user wrote
1335 // one (or misspelled another mnemonic).
1336
1337 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001338 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001339
1340 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001341 while (Next != StringRef::npos) {
1342 Start = Next;
1343 Next = Name.find('.', Start + 1);
1344 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001345
Chris Lattner3a697562010-10-28 17:20:03 +00001346 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001347 }
1348
1349 // Read the remaining operands.
1350 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001351 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001352 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001353 Parser.EatToEndOfStatement();
1354 return true;
1355 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001356
1357 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001358 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001359
1360 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001361 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001362 Parser.EatToEndOfStatement();
1363 return true;
1364 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001365 }
1366 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001367
Chris Lattnercbf8a982010-09-11 16:18:25 +00001368 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1369 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001370 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001371 }
Bill Wendling146018f2010-11-06 21:42:12 +00001372
Chris Lattner34e53142010-09-08 05:10:46 +00001373 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001374 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001375}
1376
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001377bool ARMAsmParser::
1378MatchAndEmitInstruction(SMLoc IDLoc,
1379 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1380 MCStreamer &Out) {
1381 MCInst Inst;
1382 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001383 MatchResultTy MatchResult, MatchResult2;
1384 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1385 if (MatchResult != Match_Success) {
1386 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1387 // that does not update the condition codes. So try adding a CCOut operand
1388 // with a value of reg0.
1389 if (MatchResult == Match_InvalidOperand) {
1390 Operands.insert(Operands.begin() + 1,
1391 ARMOperand::CreateCCOut(0,
1392 ((ARMOperand*)Operands[0])->getStartLoc()));
1393 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1394 if (MatchResult2 == Match_Success)
1395 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001396 else {
1397 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001398 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001399 delete CCOut;
1400 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001401 }
1402 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1403 // that updates the condition codes if it ends in 's'. So see if the
1404 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1405 // operand with a value of CPSR.
1406 else if(MatchResult == Match_MnemonicFail) {
1407 // Get the instruction mnemonic, which is the first token.
1408 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1409 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1410 // removed the 's' from the mnemonic for matching.
1411 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1412 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001413 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1414 Operands.erase(Operands.begin());
1415 delete OldMnemonic;
1416 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001417 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1418 Operands.insert(Operands.begin() + 1,
1419 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1420 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1421 if (MatchResult2 == Match_Success)
1422 MatchResult = Match_Success;
1423 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001424 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1425 Operands.erase(Operands.begin());
1426 delete OldMnemonic;
1427 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001428 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001429 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1430 Operands.erase(Operands.begin() + 1);
1431 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001432 }
1433 }
1434 }
1435 }
1436 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001437 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001438 Out.EmitInstruction(Inst);
1439 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001440 case Match_MissingFeature:
1441 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1442 return true;
1443 case Match_InvalidOperand: {
1444 SMLoc ErrorLoc = IDLoc;
1445 if (ErrorInfo != ~0U) {
1446 if (ErrorInfo >= Operands.size())
1447 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001448
Chris Lattnere73d4f82010-10-28 21:41:58 +00001449 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1450 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1451 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001452
Chris Lattnere73d4f82010-10-28 21:41:58 +00001453 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001454 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001455 case Match_MnemonicFail:
1456 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00001457 case Match_ConversionFail:
1458 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00001459 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001460
Eric Christopherc223e2b2010-10-29 09:26:59 +00001461 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001462 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001463}
1464
Kevin Enderby515d5092009-10-15 20:48:48 +00001465/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001466bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1467 StringRef IDVal = DirectiveID.getIdentifier();
1468 if (IDVal == ".word")
1469 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001470 else if (IDVal == ".thumb")
1471 return ParseDirectiveThumb(DirectiveID.getLoc());
1472 else if (IDVal == ".thumb_func")
1473 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1474 else if (IDVal == ".code")
1475 return ParseDirectiveCode(DirectiveID.getLoc());
1476 else if (IDVal == ".syntax")
1477 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001478 return true;
1479}
1480
1481/// ParseDirectiveWord
1482/// ::= .word [ expression (, expression)* ]
1483bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1484 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1485 for (;;) {
1486 const MCExpr *Value;
1487 if (getParser().ParseExpression(Value))
1488 return true;
1489
Chris Lattneraaec2052010-01-19 19:46:13 +00001490 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001491
1492 if (getLexer().is(AsmToken::EndOfStatement))
1493 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001494
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001495 // FIXME: Improve diagnostic.
1496 if (getLexer().isNot(AsmToken::Comma))
1497 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001498 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001499 }
1500 }
1501
Sean Callananb9a25b72010-01-19 20:27:46 +00001502 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001503 return false;
1504}
1505
Kevin Enderby515d5092009-10-15 20:48:48 +00001506/// ParseDirectiveThumb
1507/// ::= .thumb
1508bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1509 if (getLexer().isNot(AsmToken::EndOfStatement))
1510 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001511 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001512
1513 // TODO: set thumb mode
1514 // TODO: tell the MC streamer the mode
1515 // getParser().getStreamer().Emit???();
1516 return false;
1517}
1518
1519/// ParseDirectiveThumbFunc
1520/// ::= .thumbfunc symbol_name
1521bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001522 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001523 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001524 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001525 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001526 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001527 if (getLexer().isNot(AsmToken::EndOfStatement))
1528 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001529 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001530
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001531 // Mark symbol as a thumb symbol.
1532 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1533 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001534 return false;
1535}
1536
1537/// ParseDirectiveSyntax
1538/// ::= .syntax unified | divided
1539bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001540 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001541 if (Tok.isNot(AsmToken::Identifier))
1542 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001543 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001544 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001545 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001546 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00001547 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00001548 else
1549 return Error(L, "unrecognized syntax mode in .syntax directive");
1550
1551 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001552 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001553 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001554
1555 // TODO tell the MC streamer the mode
1556 // getParser().getStreamer().Emit???();
1557 return false;
1558}
1559
1560/// ParseDirectiveCode
1561/// ::= .code 16 | 32
1562bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001563 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001564 if (Tok.isNot(AsmToken::Integer))
1565 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001566 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001567 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001568 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001569 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001570 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001571 else
1572 return Error(L, "invalid operand to .code directive");
1573
1574 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001575 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001576 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001577
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001578 // FIXME: We need to be able switch subtargets at this point so that
1579 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1580 // includes Feature_IsThumb or not to match the right instructions. This is
1581 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1582 if (Val == 16){
1583 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1584 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001585 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001586 }
1587 else{
1588 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1589 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001590 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001591 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001592
Kevin Enderby515d5092009-10-15 20:48:48 +00001593 return false;
1594}
1595
Sean Callanan90b70972010-04-07 20:29:34 +00001596extern "C" void LLVMInitializeARMAsmLexer();
1597
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001598/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001599extern "C" void LLVMInitializeARMAsmParser() {
1600 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1601 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001602 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001603}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001604
Chris Lattner0692ee62010-09-06 19:11:01 +00001605#define GET_REGISTER_MATCHER
1606#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001607#include "ARMGenAsmMatcher.inc"