blob: 233599b0e73e1c3373204dc2e45081791162cb9a [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"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000027#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000028#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000029using namespace llvm;
30
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +000031/// Shift types used for register controlled shifts in ARM memory addressing.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000032enum ShiftType {
33 Lsl,
34 Lsr,
35 Asr,
36 Ror,
37 Rrx
38};
39
Chris Lattner3a697562010-10-28 17:20:03 +000040namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000041
42class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000043
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000044class ARMAsmParser : public TargetAsmParser {
45 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000046 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000047
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000049 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
50
51 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000052 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
53
Chris Lattnere5658fa2010-10-30 04:09:10 +000054 int TryParseRegister();
Bill Wendling50d0f582010-11-18 23:43:05 +000055 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
56 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
57 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
58 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &);
Evan Cheng75972122011-01-13 07:58:56 +000059 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000060 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
61 MCSymbolRefExpr::VariantKind Variant);
62
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000063
Kevin Enderby9c41fa82009-10-30 22:55:57 +000064 bool ParseMemoryOffsetReg(bool &Negative,
65 bool &OffsetRegShifted,
66 enum ShiftType &ShiftType,
67 const MCExpr *&ShiftAmount,
68 const MCExpr *&Offset,
69 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000070 int &OffsetRegNum,
71 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000072 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000073 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000074 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000075 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000076 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveSyntax(SMLoc L);
78
Chris Lattner7036f8b2010-09-29 01:42:58 +000079 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000080 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000081 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000082
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000083 /// @name Auto-generated Match Functions
84 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000085
Chris Lattner0692ee62010-09-06 19:11:01 +000086#define GET_ASSEMBLER_HEADER
87#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000088
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000089 /// }
90
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000091public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000092 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000093 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
94 // Initialize the set of available features.
95 setAvailableFeatures(ComputeAvailableFeatures(
96 &TM.getSubtarget<ARMSubtarget>()));
97 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000098
Benjamin Kramer38e59892010-07-14 22:38:02 +000099 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000100 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000101 virtual bool ParseDirective(AsmToken DirectiveID);
102};
Jim Grosbach16c74252010-10-29 14:46:02 +0000103} // end anonymous namespace
104
Chris Lattner3a697562010-10-28 17:20:03 +0000105namespace {
106
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000107/// ARMOperand - Instances of this class represent a parsed ARM machine
108/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000109class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000110 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000111 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000112 CCOut,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000113 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000114 Memory,
115 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000116 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000117 DPRRegisterList,
118 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000119 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000120 } Kind;
121
Sean Callanan76264762010-04-02 22:27:05 +0000122 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000123 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000124
125 union {
126 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000127 ARMCC::CondCodes Val;
128 } CC;
129
130 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000131 const char *Data;
132 unsigned Length;
133 } Tok;
134
135 struct {
136 unsigned RegNum;
137 } Reg;
138
Bill Wendling8155e5b2010-11-06 22:19:43 +0000139 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000140 const MCExpr *Val;
141 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000142
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000143 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000144 struct {
145 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000146 unsigned OffsetRegNum; // used when OffsetIsReg is true
147 const MCExpr *Offset; // used when OffsetIsReg is false
148 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
149 enum ShiftType ShiftType; // used when OffsetRegShifted is true
150 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000151 unsigned Preindexed : 1;
152 unsigned Postindexed : 1;
153 unsigned OffsetIsReg : 1;
154 unsigned Negative : 1; // only used when OffsetIsReg is true
155 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000156 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000157 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000158
Bill Wendling146018f2010-11-06 21:42:12 +0000159 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
160public:
Sean Callanan76264762010-04-02 22:27:05 +0000161 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
162 Kind = o.Kind;
163 StartLoc = o.StartLoc;
164 EndLoc = o.EndLoc;
165 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000166 case CondCode:
167 CC = o.CC;
168 break;
Sean Callanan76264762010-04-02 22:27:05 +0000169 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000170 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000171 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000172 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000173 case Register:
174 Reg = o.Reg;
175 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000176 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000177 case DPRRegisterList:
178 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000179 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000180 break;
Sean Callanan76264762010-04-02 22:27:05 +0000181 case Immediate:
182 Imm = o.Imm;
183 break;
184 case Memory:
185 Mem = o.Mem;
186 break;
187 }
188 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000189
Sean Callanan76264762010-04-02 22:27:05 +0000190 /// getStartLoc - Get the location of the first token of this operand.
191 SMLoc getStartLoc() const { return StartLoc; }
192 /// getEndLoc - Get the location of the last token of this operand.
193 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000194
Daniel Dunbar8462b302010-08-11 06:36:53 +0000195 ARMCC::CondCodes getCondCode() const {
196 assert(Kind == CondCode && "Invalid access!");
197 return CC.Val;
198 }
199
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000200 StringRef getToken() const {
201 assert(Kind == Token && "Invalid access!");
202 return StringRef(Tok.Data, Tok.Length);
203 }
204
205 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000206 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000207 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000208 }
209
Bill Wendling5fa22a12010-11-09 23:28:44 +0000210 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000211 assert((Kind == RegisterList || Kind == DPRRegisterList ||
212 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000213 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000214 }
215
Kevin Enderbycfe07242009-10-13 22:19:02 +0000216 const MCExpr *getImm() const {
217 assert(Kind == Immediate && "Invalid access!");
218 return Imm.Val;
219 }
220
Daniel Dunbar8462b302010-08-11 06:36:53 +0000221 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000222 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000223 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000224 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000225 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000226 bool isDPRRegList() const { return Kind == DPRRegisterList; }
227 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000228 bool isToken() const { return Kind == Token; }
229 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000230 bool isMemMode5() const {
231 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
232 Mem.Writeback || Mem.Negative)
233 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000234
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000235 // If there is an offset expression, make sure it's valid.
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000236 if (!Mem.Offset) return true;
237
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000238 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000239 if (!CE) return false;
240
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000241 // The offset must be a multiple of 4 in the range 0-1020.
242 int64_t Value = CE->getValue();
243 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
244 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000245 bool isMemModeRegThumb() const {
246 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
247 return false;
248 return !Mem.Offset || !isa<MCConstantExpr>(Mem.Offset);
249 }
250 bool isMemModeImmThumb() const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000251 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
252 return false;
253
Bill Wendlingf4caf692010-12-14 03:36:38 +0000254 if (!Mem.Offset) return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000255
256 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
257 if (!CE) return false;
258
259 // The offset must be a multiple of 4 in the range 0-124.
260 uint64_t Value = CE->getValue();
261 return ((Value & 0x3) == 0 && Value <= 124);
262 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000263
264 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000265 // Add as immediates when possible. Null MCExpr = 0.
266 if (Expr == 0)
267 Inst.addOperand(MCOperand::CreateImm(0));
268 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000269 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
270 else
271 Inst.addOperand(MCOperand::CreateExpr(Expr));
272 }
273
Daniel Dunbar8462b302010-08-11 06:36:53 +0000274 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000275 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000276 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000277 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
278 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000279 }
280
Jim Grosbachd67641b2010-12-06 18:21:12 +0000281 void addCCOutOperands(MCInst &Inst, unsigned N) const {
282 assert(N == 1 && "Invalid number of operands!");
283 Inst.addOperand(MCOperand::CreateReg(getReg()));
284 }
285
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000286 void addRegOperands(MCInst &Inst, unsigned N) const {
287 assert(N == 1 && "Invalid number of operands!");
288 Inst.addOperand(MCOperand::CreateReg(getReg()));
289 }
290
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000291 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000292 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000293 const SmallVectorImpl<unsigned> &RegList = getRegList();
294 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000295 I = RegList.begin(), E = RegList.end(); I != E; ++I)
296 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000297 }
298
Bill Wendling0f630752010-11-17 04:32:08 +0000299 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
300 addRegListOperands(Inst, N);
301 }
302
303 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
304 addRegListOperands(Inst, N);
305 }
306
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000307 void addImmOperands(MCInst &Inst, unsigned N) const {
308 assert(N == 1 && "Invalid number of operands!");
309 addExpr(Inst, getImm());
310 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000311
Chris Lattner14b93852010-10-29 00:27:31 +0000312 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
313 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000314
Chris Lattner14b93852010-10-29 00:27:31 +0000315 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000316 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000317
Jim Grosbach80eb2332010-10-29 17:41:25 +0000318 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
319 // the difference?
320 if (Mem.Offset) {
321 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000322 assert(CE && "Non-constant mode 5 offset operand!");
323
Jim Grosbach80eb2332010-10-29 17:41:25 +0000324 // The MCInst offset operand doesn't include the low two bits (like
325 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000326 int64_t Offset = CE->getValue() / 4;
327 if (Offset >= 0)
328 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
329 Offset)));
330 else
331 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
332 -Offset)));
333 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000334 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000335 }
Chris Lattner14b93852010-10-29 00:27:31 +0000336 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000337
Bill Wendlingf4caf692010-12-14 03:36:38 +0000338 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
339 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000340 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000341 Inst.addOperand(MCOperand::CreateReg(Mem.OffsetRegNum));
342 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000343
Bill Wendlingf4caf692010-12-14 03:36:38 +0000344 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
345 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
346 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
347 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
348 assert(CE && "Non-constant mode offset operand!");
349 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000350 }
351
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000352 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000353
Chris Lattner3a697562010-10-28 17:20:03 +0000354 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
355 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000356 Op->CC.Val = CC;
357 Op->StartLoc = S;
358 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000359 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000360 }
361
Jim Grosbachd67641b2010-12-06 18:21:12 +0000362 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
363 ARMOperand *Op = new ARMOperand(CCOut);
364 Op->Reg.RegNum = RegNum;
365 Op->StartLoc = S;
366 Op->EndLoc = S;
367 return Op;
368 }
369
Chris Lattner3a697562010-10-28 17:20:03 +0000370 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
371 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000372 Op->Tok.Data = Str.data();
373 Op->Tok.Length = Str.size();
374 Op->StartLoc = S;
375 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000376 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000377 }
378
Bill Wendling50d0f582010-11-18 23:43:05 +0000379 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000380 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000381 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000382 Op->StartLoc = S;
383 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000384 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000385 }
386
Bill Wendling7729e062010-11-09 22:44:22 +0000387 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000388 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000389 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000390 KindTy Kind = RegisterList;
391
392 if (ARM::DPRRegClass.contains(Regs.front().first))
393 Kind = DPRRegisterList;
394 else if (ARM::SPRRegClass.contains(Regs.front().first))
395 Kind = SPRRegisterList;
396
397 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000398 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000399 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000400 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000401 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000402 Op->StartLoc = StartLoc;
403 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000404 return Op;
405 }
406
Chris Lattner3a697562010-10-28 17:20:03 +0000407 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
408 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000409 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000410 Op->StartLoc = S;
411 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000412 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000413 }
414
Chris Lattner3a697562010-10-28 17:20:03 +0000415 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
416 const MCExpr *Offset, unsigned OffsetRegNum,
417 bool OffsetRegShifted, enum ShiftType ShiftType,
418 const MCExpr *ShiftAmount, bool Preindexed,
419 bool Postindexed, bool Negative, bool Writeback,
420 SMLoc S, SMLoc E) {
421 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000422 Op->Mem.BaseRegNum = BaseRegNum;
423 Op->Mem.OffsetIsReg = OffsetIsReg;
424 Op->Mem.Offset = Offset;
425 Op->Mem.OffsetRegNum = OffsetRegNum;
426 Op->Mem.OffsetRegShifted = OffsetRegShifted;
427 Op->Mem.ShiftType = ShiftType;
428 Op->Mem.ShiftAmount = ShiftAmount;
429 Op->Mem.Preindexed = Preindexed;
430 Op->Mem.Postindexed = Postindexed;
431 Op->Mem.Negative = Negative;
432 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000433
Sean Callanan76264762010-04-02 22:27:05 +0000434 Op->StartLoc = S;
435 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000436 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000437 }
438};
439
440} // end anonymous namespace.
441
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000442void ARMOperand::dump(raw_ostream &OS) const {
443 switch (Kind) {
444 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000445 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000446 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000447 case CCOut:
448 OS << "<ccout " << getReg() << ">";
449 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000450 case Immediate:
451 getImm()->print(OS);
452 break;
453 case Memory:
Bill Wendling50d0f582010-11-18 23:43:05 +0000454 OS << "<memory>";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000455 break;
456 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000457 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000458 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000459 case RegisterList:
460 case DPRRegisterList:
461 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000462 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000463
Bill Wendling5fa22a12010-11-09 23:28:44 +0000464 const SmallVectorImpl<unsigned> &RegList = getRegList();
465 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000466 I = RegList.begin(), E = RegList.end(); I != E; ) {
467 OS << *I;
468 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000469 }
470
471 OS << ">";
472 break;
473 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000474 case Token:
475 OS << "'" << getToken() << "'";
476 break;
477 }
478}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000479
480/// @name Auto-generated Match Functions
481/// {
482
483static unsigned MatchRegisterName(StringRef Name);
484
485/// }
486
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000487/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000488/// and if it is a register name the token is eaten and the register number is
489/// returned. Otherwise return -1.
490///
491int ARMAsmParser::TryParseRegister() {
492 const AsmToken &Tok = Parser.getTok();
493 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000494
Chris Lattnere5658fa2010-10-30 04:09:10 +0000495 // FIXME: Validate register for the current architecture; we have to do
496 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000497 unsigned RegNum = MatchRegisterName(Tok.getString());
498 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000499 return -1;
500 Parser.Lex(); // Eat identifier token.
501 return RegNum;
502}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000503
504
Bill Wendling50d0f582010-11-18 23:43:05 +0000505/// Try to parse a register name. The token must be an Identifier when called.
506/// If it's a register, an AsmOperand is created. Another AsmOperand is created
507/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000508///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000509/// TODO this is likely to change to allow different register types and or to
510/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000511bool ARMAsmParser::
512TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000513 SMLoc S = Parser.getTok().getLoc();
514 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000515 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000516 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000517
Bill Wendling50d0f582010-11-18 23:43:05 +0000518 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000519
Chris Lattnere5658fa2010-10-30 04:09:10 +0000520 const AsmToken &ExclaimTok = Parser.getTok();
521 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000522 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
523 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000524 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000525 }
526
Bill Wendling50d0f582010-11-18 23:43:05 +0000527 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000528}
529
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000530/// Parse a register list, return it if successful else return null. The first
531/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000532bool ARMAsmParser::
533ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000534 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000535 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000536 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000537
Bill Wendling7729e062010-11-09 22:44:22 +0000538 // Read the rest of the registers in the list.
539 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000540 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000541
Bill Wendling7729e062010-11-09 22:44:22 +0000542 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000543 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000544 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000545
Sean Callanan18b83232010-01-19 21:44:56 +0000546 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000547 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000548 if (RegTok.isNot(AsmToken::Identifier)) {
549 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000550 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000551 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000552
Bill Wendling1d6a2652010-11-06 10:40:24 +0000553 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000554 if (RegNum == -1) {
555 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000556 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000557 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000558
Bill Wendlinge7176102010-11-06 22:36:58 +0000559 if (IsRange) {
560 int Reg = PrevRegNum;
561 do {
562 ++Reg;
563 Registers.push_back(std::make_pair(Reg, RegLoc));
564 } while (Reg != RegNum);
565 } else {
566 Registers.push_back(std::make_pair(RegNum, RegLoc));
567 }
568
569 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000570 } while (Parser.getTok().is(AsmToken::Comma) ||
571 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000572
573 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000574 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000575 if (RCurlyTok.isNot(AsmToken::RCurly)) {
576 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000577 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000578 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000579
Bill Wendlinge7176102010-11-06 22:36:58 +0000580 SMLoc E = RCurlyTok.getLoc();
581 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000582
Bill Wendlinge7176102010-11-06 22:36:58 +0000583 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000584 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000585 RI = Registers.begin(), RE = Registers.end();
586
Bill Wendling7caebff2011-01-12 21:20:59 +0000587 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000588 bool EmittedWarning = false;
589
Bill Wendling7caebff2011-01-12 21:20:59 +0000590 DenseMap<unsigned, bool> RegMap;
591 RegMap[HighRegNum] = true;
592
Bill Wendlinge7176102010-11-06 22:36:58 +0000593 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000594 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000595 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000596
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000597 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000598 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000599 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000600 }
601
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000602 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000603 Warning(RegInfo.second,
604 "register not in ascending order in register list");
605
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000606 RegMap[Reg] = true;
607 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000608 }
609
Bill Wendling50d0f582010-11-18 23:43:05 +0000610 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
611 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000612}
613
Bill Wendlinge7176102010-11-06 22:36:58 +0000614/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000615/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000616///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000617/// TODO Only preindexing and postindexing addressing are started, unindexed
618/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000619bool ARMAsmParser::
620ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000621 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000622 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000623 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000624 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000625 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000626
Sean Callanan18b83232010-01-19 21:44:56 +0000627 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000628 if (BaseRegTok.isNot(AsmToken::Identifier)) {
629 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000630 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000631 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000632 int BaseRegNum = TryParseRegister();
633 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000634 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000635 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000636 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000637
638 bool Preindexed = false;
639 bool Postindexed = false;
640 bool OffsetIsReg = false;
641 bool Negative = false;
642 bool Writeback = false;
643
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000644 // First look for preindexed address forms, that is after the "[Rn" we now
645 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000646 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000647 if (Tok.is(AsmToken::Comma)) {
648 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000649 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000650 int OffsetRegNum;
651 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000652 enum ShiftType ShiftType;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000653 const MCExpr *ShiftAmount = 0;
654 const MCExpr *Offset = 0;
Chris Lattner550276e2010-10-28 20:52:15 +0000655 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
656 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000657 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000658 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000659 if (RBracTok.isNot(AsmToken::RBrac)) {
660 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000661 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000662 }
Sean Callanan76264762010-04-02 22:27:05 +0000663 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000664 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000665
Jim Grosbach03f44a02010-11-29 23:18:01 +0000666
Sean Callanan18b83232010-01-19 21:44:56 +0000667 const AsmToken &ExclaimTok = Parser.getTok();
Bill Wendling50d0f582010-11-18 23:43:05 +0000668 ARMOperand *WBOp = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000669 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000670 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
671 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000672 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000673 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000674 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000675
676 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
677 OffsetRegNum, OffsetRegShifted,
678 ShiftType, ShiftAmount, Preindexed,
679 Postindexed, Negative, Writeback,
680 S, E));
681 if (WBOp)
682 Operands.push_back(WBOp);
683
684 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000685 }
686 // The "[Rn" we have so far was not followed by a comma.
687 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000688 // If there's anything other than the right brace, this is a post indexing
689 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000690 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000691 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000692
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000693 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000694 bool OffsetRegShifted = false;
Jim Grosbach00a257a2010-11-29 23:41:10 +0000695 enum ShiftType ShiftType = Lsl;
696 const MCExpr *ShiftAmount = 0;
Chris Lattner14b93852010-10-29 00:27:31 +0000697 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000698
Sean Callanan18b83232010-01-19 21:44:56 +0000699 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000700
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000701 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000702 Postindexed = true;
703 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000704
Chris Lattner550276e2010-10-28 20:52:15 +0000705 if (NextTok.isNot(AsmToken::Comma)) {
706 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000707 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000708 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000709
Sean Callananb9a25b72010-01-19 20:27:46 +0000710 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000711
Chris Lattner550276e2010-10-28 20:52:15 +0000712 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000713 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000714 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000715 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000716 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000717
Bill Wendling50d0f582010-11-18 23:43:05 +0000718 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
719 OffsetRegNum, OffsetRegShifted,
720 ShiftType, ShiftAmount, Preindexed,
721 Postindexed, Negative, Writeback,
722 S, E));
723 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000724 }
725
Bill Wendling50d0f582010-11-18 23:43:05 +0000726 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000727}
728
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000729/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
730/// we will parse the following (were +/- means that a plus or minus is
731/// optional):
732/// +/-Rm
733/// +/-Rm, shift
734/// #offset
735/// we return false on success or an error otherwise.
736bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000737 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000738 enum ShiftType &ShiftType,
739 const MCExpr *&ShiftAmount,
740 const MCExpr *&Offset,
741 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000742 int &OffsetRegNum,
743 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000744 Negative = false;
745 OffsetRegShifted = false;
746 OffsetIsReg = false;
747 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000748 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000749 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000750 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000751 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000752 else if (NextTok.is(AsmToken::Minus)) {
753 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000754 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000755 }
756 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000757 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000758 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000759 SMLoc CurLoc = OffsetRegTok.getLoc();
760 OffsetRegNum = TryParseRegister();
761 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000762 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000763 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000764 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000765 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000766
Bill Wendling12f40e92010-11-06 10:51:53 +0000767 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000768 if (OffsetRegNum != -1) {
769 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000770 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000771 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000772 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000773
Sean Callanan18b83232010-01-19 21:44:56 +0000774 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000775 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000776 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000777 OffsetRegShifted = true;
778 }
779 }
780 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
781 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000782 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000783 if (HashTok.isNot(AsmToken::Hash))
784 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000785
Sean Callananb9a25b72010-01-19 20:27:46 +0000786 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000787
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000788 if (getParser().ParseExpression(Offset))
789 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000790 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000791 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000792 return false;
793}
794
795/// ParseShift as one of these two:
796/// ( lsl | lsr | asr | ror ) , # shift_amount
797/// rrx
798/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000799bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000800 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000801 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000802 if (Tok.isNot(AsmToken::Identifier))
803 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000804 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000805 if (ShiftName == "lsl" || ShiftName == "LSL")
806 St = Lsl;
807 else if (ShiftName == "lsr" || ShiftName == "LSR")
808 St = Lsr;
809 else if (ShiftName == "asr" || ShiftName == "ASR")
810 St = Asr;
811 else if (ShiftName == "ror" || ShiftName == "ROR")
812 St = Ror;
813 else if (ShiftName == "rrx" || ShiftName == "RRX")
814 St = Rrx;
815 else
816 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000817 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000818
819 // Rrx stands alone.
820 if (St == Rrx)
821 return false;
822
823 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000824 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000825 if (HashTok.isNot(AsmToken::Hash))
826 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000827 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000828
829 if (getParser().ParseExpression(ShiftAmount))
830 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000831
832 return false;
833}
834
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000835/// Parse a arm instruction operand. For now this parses the operand regardless
836/// of the mnemonic.
Bill Wendling50d0f582010-11-18 23:43:05 +0000837bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands){
Sean Callanan76264762010-04-02 22:27:05 +0000838 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000839 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000840 default:
841 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000842 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +0000843 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +0000844 if (!TryParseRegisterWithWriteBack(Operands))
845 return false;
Kevin Enderby67b212e2011-01-13 20:32:36 +0000846 // Fall though for the Identifier case that is not a register
847 case AsmToken::Integer: // things like 1f and 2b as a branch targets
848 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +0000849 // This was not a register so parse other operands that start with an
850 // identifier (like labels) as expressions and create them as immediates.
851 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000852 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000853 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000854 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000855 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000856 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
857 return false;
858 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000859 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000860 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000861 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000862 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000863 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000864 // #42 -> immediate.
865 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000866 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000867 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000868 const MCExpr *ImmVal;
869 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000870 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000871 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000872 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
873 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000874 case AsmToken::Colon: {
875 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +0000876 // FIXME: Check it's an expression prefix,
877 // e.g. (FOO - :lower16:BAR) isn't legal.
878 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000879 if (ParsePrefix(RefKind))
880 return true;
881
Evan Cheng75972122011-01-13 07:58:56 +0000882 const MCExpr *SubExprVal;
883 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +0000884 return true;
885
Evan Cheng75972122011-01-13 07:58:56 +0000886 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
887 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +0000888 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +0000889 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +0000890 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000891 }
Jason W Kim9081b4b2011-01-11 23:53:41 +0000892 }
893}
894
Evan Cheng75972122011-01-13 07:58:56 +0000895// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
896// :lower16: and :upper16:.
897bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
898 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000899
900 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +0000901 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +0000902 Parser.Lex(); // Eat ':'
903
904 if (getLexer().isNot(AsmToken::Identifier)) {
905 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
906 return true;
907 }
908
909 StringRef IDVal = Parser.getTok().getIdentifier();
910 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +0000911 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000912 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +0000913 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000914 } else {
915 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
916 return true;
917 }
918 Parser.Lex();
919
920 if (getLexer().isNot(AsmToken::Colon)) {
921 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
922 return true;
923 }
924 Parser.Lex(); // Eat the last ':'
925 return false;
926}
927
928const MCExpr *
929ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
930 MCSymbolRefExpr::VariantKind Variant) {
931 // Recurse over the given expression, rebuilding it to apply the given variant
932 // to the leftmost symbol.
933 if (Variant == MCSymbolRefExpr::VK_None)
934 return E;
935
936 switch (E->getKind()) {
937 case MCExpr::Target:
938 llvm_unreachable("Can't handle target expr yet");
939 case MCExpr::Constant:
940 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
941
942 case MCExpr::SymbolRef: {
943 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
944
945 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
946 return 0;
947
948 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
949 }
950
951 case MCExpr::Unary:
952 llvm_unreachable("Can't handle unary expressions yet");
953
954 case MCExpr::Binary: {
955 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
956 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
957 const MCExpr *RHS = BE->getRHS();
958 if (!LHS)
959 return 0;
960
961 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
962 }
963 }
964
965 assert(0 && "Invalid expression kind!");
966 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000967}
968
Daniel Dunbar352e1482011-01-11 15:59:50 +0000969/// \brief Given a mnemonic, split out possible predication code and carry
970/// setting letters to form a canonical mnemonic and flags.
971//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000972// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +0000973static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
974 unsigned &PredicationCode,
975 bool &CarrySetting) {
976 PredicationCode = ARMCC::AL;
977 CarrySetting = false;
978
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000979 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +0000980 //
981 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +0000982 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
983 Mnemonic == "movs" ||
984 Mnemonic == "svc" ||
985 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
986 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
987 Mnemonic == "vacge" || Mnemonic == "vcge" ||
988 Mnemonic == "vclt" ||
989 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
990 Mnemonic == "vcle" ||
991 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
992 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
993 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +0000994 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +0000995
Daniel Dunbar352e1482011-01-11 15:59:50 +0000996 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000997 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000998 .Case("eq", ARMCC::EQ)
999 .Case("ne", ARMCC::NE)
1000 .Case("hs", ARMCC::HS)
1001 .Case("lo", ARMCC::LO)
1002 .Case("mi", ARMCC::MI)
1003 .Case("pl", ARMCC::PL)
1004 .Case("vs", ARMCC::VS)
1005 .Case("vc", ARMCC::VC)
1006 .Case("hi", ARMCC::HI)
1007 .Case("ls", ARMCC::LS)
1008 .Case("ge", ARMCC::GE)
1009 .Case("lt", ARMCC::LT)
1010 .Case("gt", ARMCC::GT)
1011 .Case("le", ARMCC::LE)
1012 .Case("al", ARMCC::AL)
1013 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001014 if (CC != ~0U) {
1015 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001016 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001017 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001018
Daniel Dunbar352e1482011-01-11 15:59:50 +00001019 // Next, determine if we have a carry setting bit. We explicitly ignore all
1020 // the instructions we know end in 's'.
1021 if (Mnemonic.endswith("s") &&
1022 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1023 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1024 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1025 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1026 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1027 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1028 CarrySetting = true;
1029 }
1030
1031 return Mnemonic;
1032}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001033
1034/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1035/// inclusion of carry set or predication code operands.
1036//
1037// FIXME: It would be nice to autogen this.
1038static void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1039 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001040 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1041 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1042 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1043 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1044 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1045 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1046 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1047 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1048 CanAcceptCarrySet = true;
1049 } else {
1050 CanAcceptCarrySet = false;
1051 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001052
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001053 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1054 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1055 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1056 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
1057 Mnemonic == "dsb" || Mnemonic == "movs") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001058 CanAcceptPredicationCode = false;
1059 } else {
1060 CanAcceptPredicationCode = true;
1061 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001062}
1063
1064/// Parse an arm instruction mnemonic followed by its operands.
1065bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1066 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1067 // Create the leading tokens for the mnemonic, split by '.' characters.
1068 size_t Start = 0, Next = Name.find('.');
1069 StringRef Head = Name.slice(Start, Next);
1070
Daniel Dunbar352e1482011-01-11 15:59:50 +00001071 // Split out the predication code and carry setting flag from the mnemonic.
1072 unsigned PredicationCode;
1073 bool CarrySetting;
1074 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001075
Chris Lattner3a697562010-10-28 17:20:03 +00001076 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001077
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001078 // Next, add the CCOut and ConditionCode operands, if needed.
1079 //
1080 // For mnemonics which can ever incorporate a carry setting bit or predication
1081 // code, our matching model involves us always generating CCOut and
1082 // ConditionCode operands to match the mnemonic "as written" and then we let
1083 // the matcher deal with finding the right instruction or generating an
1084 // appropriate error.
1085 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1086 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1087
1088 // Add the carry setting operand, if necessary.
1089 //
1090 // FIXME: It would be awesome if we could somehow invent a location such that
1091 // match errors on this operand would print a nice diagnostic about how the
1092 // 's' character in the mnemonic resulted in a CCOut operand.
1093 if (CanAcceptCarrySet) {
1094 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1095 NameLoc));
1096 } else {
1097 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1098 // misspelled another mnemonic).
1099
1100 // FIXME: Issue a nice error.
1101 }
1102
1103 // Add the predication code operand, if necessary.
1104 if (CanAcceptPredicationCode) {
1105 Operands.push_back(ARMOperand::CreateCondCode(
1106 ARMCC::CondCodes(PredicationCode), NameLoc));
1107 } else {
1108 // This mnemonic can't ever accept a predication code, but the user wrote
1109 // one (or misspelled another mnemonic).
1110
1111 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001112 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001113
1114 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001115 while (Next != StringRef::npos) {
1116 Start = Next;
1117 Next = Name.find('.', Start + 1);
1118 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001119
Chris Lattner3a697562010-10-28 17:20:03 +00001120 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001121 }
1122
1123 // Read the remaining operands.
1124 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001125 // Read the first operand.
Bill Wendling50d0f582010-11-18 23:43:05 +00001126 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001127 Parser.EatToEndOfStatement();
1128 return true;
1129 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001130
1131 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001132 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001133
1134 // Parse and remember the operand.
Bill Wendling50d0f582010-11-18 23:43:05 +00001135 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001136 Parser.EatToEndOfStatement();
1137 return true;
1138 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001139 }
1140 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001141
Chris Lattnercbf8a982010-09-11 16:18:25 +00001142 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1143 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001144 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001145 }
Bill Wendling146018f2010-11-06 21:42:12 +00001146
Chris Lattner34e53142010-09-08 05:10:46 +00001147 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001148 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001149}
1150
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001151bool ARMAsmParser::
1152MatchAndEmitInstruction(SMLoc IDLoc,
1153 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1154 MCStreamer &Out) {
1155 MCInst Inst;
1156 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001157 MatchResultTy MatchResult, MatchResult2;
1158 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1159 if (MatchResult != Match_Success) {
1160 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1161 // that does not update the condition codes. So try adding a CCOut operand
1162 // with a value of reg0.
1163 if (MatchResult == Match_InvalidOperand) {
1164 Operands.insert(Operands.begin() + 1,
1165 ARMOperand::CreateCCOut(0,
1166 ((ARMOperand*)Operands[0])->getStartLoc()));
1167 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1168 if (MatchResult2 == Match_Success)
1169 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001170 else {
1171 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001172 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001173 delete CCOut;
1174 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001175 }
1176 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1177 // that updates the condition codes if it ends in 's'. So see if the
1178 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1179 // operand with a value of CPSR.
1180 else if(MatchResult == Match_MnemonicFail) {
1181 // Get the instruction mnemonic, which is the first token.
1182 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1183 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1184 // removed the 's' from the mnemonic for matching.
1185 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1186 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001187 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1188 Operands.erase(Operands.begin());
1189 delete OldMnemonic;
1190 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001191 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1192 Operands.insert(Operands.begin() + 1,
1193 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1194 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1195 if (MatchResult2 == Match_Success)
1196 MatchResult = Match_Success;
1197 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001198 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1199 Operands.erase(Operands.begin());
1200 delete OldMnemonic;
1201 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001202 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001203 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1204 Operands.erase(Operands.begin() + 1);
1205 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001206 }
1207 }
1208 }
1209 }
1210 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001211 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001212 Out.EmitInstruction(Inst);
1213 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001214 case Match_MissingFeature:
1215 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1216 return true;
1217 case Match_InvalidOperand: {
1218 SMLoc ErrorLoc = IDLoc;
1219 if (ErrorInfo != ~0U) {
1220 if (ErrorInfo >= Operands.size())
1221 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001222
Chris Lattnere73d4f82010-10-28 21:41:58 +00001223 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1224 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1225 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001226
Chris Lattnere73d4f82010-10-28 21:41:58 +00001227 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001228 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001229 case Match_MnemonicFail:
1230 return Error(IDLoc, "unrecognized instruction mnemonic");
1231 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001232
Eric Christopherc223e2b2010-10-29 09:26:59 +00001233 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001234 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001235}
1236
Kevin Enderby515d5092009-10-15 20:48:48 +00001237/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001238bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1239 StringRef IDVal = DirectiveID.getIdentifier();
1240 if (IDVal == ".word")
1241 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001242 else if (IDVal == ".thumb")
1243 return ParseDirectiveThumb(DirectiveID.getLoc());
1244 else if (IDVal == ".thumb_func")
1245 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1246 else if (IDVal == ".code")
1247 return ParseDirectiveCode(DirectiveID.getLoc());
1248 else if (IDVal == ".syntax")
1249 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001250 return true;
1251}
1252
1253/// ParseDirectiveWord
1254/// ::= .word [ expression (, expression)* ]
1255bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1256 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1257 for (;;) {
1258 const MCExpr *Value;
1259 if (getParser().ParseExpression(Value))
1260 return true;
1261
Chris Lattneraaec2052010-01-19 19:46:13 +00001262 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001263
1264 if (getLexer().is(AsmToken::EndOfStatement))
1265 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001266
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001267 // FIXME: Improve diagnostic.
1268 if (getLexer().isNot(AsmToken::Comma))
1269 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001270 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001271 }
1272 }
1273
Sean Callananb9a25b72010-01-19 20:27:46 +00001274 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001275 return false;
1276}
1277
Kevin Enderby515d5092009-10-15 20:48:48 +00001278/// ParseDirectiveThumb
1279/// ::= .thumb
1280bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1281 if (getLexer().isNot(AsmToken::EndOfStatement))
1282 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001283 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001284
1285 // TODO: set thumb mode
1286 // TODO: tell the MC streamer the mode
1287 // getParser().getStreamer().Emit???();
1288 return false;
1289}
1290
1291/// ParseDirectiveThumbFunc
1292/// ::= .thumbfunc symbol_name
1293bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001294 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001295 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001296 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001297 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001298 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001299 if (getLexer().isNot(AsmToken::EndOfStatement))
1300 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001301 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001302
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001303 // Mark symbol as a thumb symbol.
1304 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1305 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001306 return false;
1307}
1308
1309/// ParseDirectiveSyntax
1310/// ::= .syntax unified | divided
1311bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001312 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001313 if (Tok.isNot(AsmToken::Identifier))
1314 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001315 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001316 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001317 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001318 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001319 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001320 else
1321 return Error(L, "unrecognized syntax mode in .syntax directive");
1322
1323 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001324 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001325 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001326
1327 // TODO tell the MC streamer the mode
1328 // getParser().getStreamer().Emit???();
1329 return false;
1330}
1331
1332/// ParseDirectiveCode
1333/// ::= .code 16 | 32
1334bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001335 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001336 if (Tok.isNot(AsmToken::Integer))
1337 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001338 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001339 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001340 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001341 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001342 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001343 else
1344 return Error(L, "invalid operand to .code directive");
1345
1346 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001347 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001348 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001349
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001350 // FIXME: We need to be able switch subtargets at this point so that
1351 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1352 // includes Feature_IsThumb or not to match the right instructions. This is
1353 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1354 if (Val == 16){
1355 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1356 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001357 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001358 }
1359 else{
1360 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1361 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001362 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001363 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001364
Kevin Enderby515d5092009-10-15 20:48:48 +00001365 return false;
1366}
1367
Sean Callanan90b70972010-04-07 20:29:34 +00001368extern "C" void LLVMInitializeARMAsmLexer();
1369
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001370/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001371extern "C" void LLVMInitializeARMAsmParser() {
1372 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1373 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001374 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001375}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001376
Chris Lattner0692ee62010-09-06 19:11:01 +00001377#define GET_REGISTER_MATCHER
1378#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001379#include "ARMGenAsmMatcher.inc"