blob: 28fdb60f8a084c8daea2b84b5a12cede038f027c [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);
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +000057 bool TryParseCoprocessorOperandName(SmallVectorImpl<MCParsedAsmOperand*>&);
Bill Wendling50d0f582010-11-18 23:43:05 +000058 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
59 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
60 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +000061 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, bool hasCoprocOp);
Evan Cheng75972122011-01-13 07:58:56 +000062 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000063 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
64 MCSymbolRefExpr::VariantKind Variant);
65
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000066
Kevin Enderby9c41fa82009-10-30 22:55:57 +000067 bool ParseMemoryOffsetReg(bool &Negative,
68 bool &OffsetRegShifted,
69 enum ShiftType &ShiftType,
70 const MCExpr *&ShiftAmount,
71 const MCExpr *&Offset,
72 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000073 int &OffsetRegNum,
74 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000075 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000076 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000078 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000080 bool ParseDirectiveSyntax(SMLoc L);
81
Chris Lattner7036f8b2010-09-29 01:42:58 +000082 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000083 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000084 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000085 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
86 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000087
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000088 /// @name Auto-generated Match Functions
89 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000090
Chris Lattner0692ee62010-09-06 19:11:01 +000091#define GET_ASSEMBLER_HEADER
92#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000093
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000094 /// }
95
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000096public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000097 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000098 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
99 // Initialize the set of available features.
100 setAvailableFeatures(ComputeAvailableFeatures(
101 &TM.getSubtarget<ARMSubtarget>()));
102 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000103
Benjamin Kramer38e59892010-07-14 22:38:02 +0000104 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000105 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000106 virtual bool ParseDirective(AsmToken DirectiveID);
107};
Jim Grosbach16c74252010-10-29 14:46:02 +0000108} // end anonymous namespace
109
Chris Lattner3a697562010-10-28 17:20:03 +0000110namespace {
111
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000112/// ARMOperand - Instances of this class represent a parsed ARM machine
113/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000114class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000115 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000116 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000117 CCOut,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000118 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000119 Memory,
120 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000121 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000122 DPRRegisterList,
123 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000124 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000125 } Kind;
126
Sean Callanan76264762010-04-02 22:27:05 +0000127 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000128 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000129
130 union {
131 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000132 ARMCC::CondCodes Val;
133 } CC;
134
135 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000136 const char *Data;
137 unsigned Length;
138 } Tok;
139
140 struct {
141 unsigned RegNum;
142 } Reg;
143
Bill Wendling8155e5b2010-11-06 22:19:43 +0000144 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000145 const MCExpr *Val;
146 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000147
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000148 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000149 struct {
150 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000151 union {
152 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
153 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
154 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000155 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
156 enum ShiftType ShiftType; // used when OffsetRegShifted is true
157 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000158 unsigned Preindexed : 1;
159 unsigned Postindexed : 1;
160 unsigned OffsetIsReg : 1;
161 unsigned Negative : 1; // only used when OffsetIsReg is true
162 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000163 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000164 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000165
Bill Wendling146018f2010-11-06 21:42:12 +0000166 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
167public:
Sean Callanan76264762010-04-02 22:27:05 +0000168 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
169 Kind = o.Kind;
170 StartLoc = o.StartLoc;
171 EndLoc = o.EndLoc;
172 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000173 case CondCode:
174 CC = o.CC;
175 break;
Sean Callanan76264762010-04-02 22:27:05 +0000176 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000177 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000178 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000179 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000180 case Register:
181 Reg = o.Reg;
182 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000183 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000184 case DPRRegisterList:
185 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000186 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000187 break;
Sean Callanan76264762010-04-02 22:27:05 +0000188 case Immediate:
189 Imm = o.Imm;
190 break;
191 case Memory:
192 Mem = o.Mem;
193 break;
194 }
195 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000196
Sean Callanan76264762010-04-02 22:27:05 +0000197 /// getStartLoc - Get the location of the first token of this operand.
198 SMLoc getStartLoc() const { return StartLoc; }
199 /// getEndLoc - Get the location of the last token of this operand.
200 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000201
Daniel Dunbar8462b302010-08-11 06:36:53 +0000202 ARMCC::CondCodes getCondCode() const {
203 assert(Kind == CondCode && "Invalid access!");
204 return CC.Val;
205 }
206
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000207 StringRef getToken() const {
208 assert(Kind == Token && "Invalid access!");
209 return StringRef(Tok.Data, Tok.Length);
210 }
211
212 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000213 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000214 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000215 }
216
Bill Wendling5fa22a12010-11-09 23:28:44 +0000217 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000218 assert((Kind == RegisterList || Kind == DPRRegisterList ||
219 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000220 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000221 }
222
Kevin Enderbycfe07242009-10-13 22:19:02 +0000223 const MCExpr *getImm() const {
224 assert(Kind == Immediate && "Invalid access!");
225 return Imm.Val;
226 }
227
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000228 /// @name Memory Operand Accessors
229 /// @{
230
231 unsigned getMemBaseRegNum() const {
232 return Mem.BaseRegNum;
233 }
234 unsigned getMemOffsetRegNum() const {
235 assert(Mem.OffsetIsReg && "Invalid access!");
236 return Mem.Offset.RegNum;
237 }
238 const MCExpr *getMemOffset() const {
239 assert(!Mem.OffsetIsReg && "Invalid access!");
240 return Mem.Offset.Value;
241 }
242 unsigned getMemOffsetRegShifted() const {
243 assert(Mem.OffsetIsReg && "Invalid access!");
244 return Mem.OffsetRegShifted;
245 }
246 const MCExpr *getMemShiftAmount() const {
247 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
248 return Mem.ShiftAmount;
249 }
250 enum ShiftType getMemShiftType() const {
251 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
252 return Mem.ShiftType;
253 }
254 bool getMemPreindexed() const { return Mem.Preindexed; }
255 bool getMemPostindexed() const { return Mem.Postindexed; }
256 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
257 bool getMemNegative() const { return Mem.Negative; }
258 bool getMemWriteback() const { return Mem.Writeback; }
259
260 /// @}
261
Daniel Dunbar8462b302010-08-11 06:36:53 +0000262 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000263 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000264 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000265 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000266 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000267 bool isDPRRegList() const { return Kind == DPRRegisterList; }
268 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000269 bool isToken() const { return Kind == Token; }
270 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000271 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000272 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
273 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000274 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000275
Daniel Dunbar4b462672011-01-18 05:55:27 +0000276 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000277 if (!CE) return false;
278
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000279 // The offset must be a multiple of 4 in the range 0-1020.
280 int64_t Value = CE->getValue();
281 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
282 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000283 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000284 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000285 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000286 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000287 }
288 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000289 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000290 return false;
291
Daniel Dunbar4b462672011-01-18 05:55:27 +0000292 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000293 if (!CE) return false;
294
295 // The offset must be a multiple of 4 in the range 0-124.
296 uint64_t Value = CE->getValue();
297 return ((Value & 0x3) == 0 && Value <= 124);
298 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000299
300 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000301 // Add as immediates when possible. Null MCExpr = 0.
302 if (Expr == 0)
303 Inst.addOperand(MCOperand::CreateImm(0));
304 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000305 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
306 else
307 Inst.addOperand(MCOperand::CreateExpr(Expr));
308 }
309
Daniel Dunbar8462b302010-08-11 06:36:53 +0000310 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000311 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000312 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000313 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
314 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000315 }
316
Jim Grosbachd67641b2010-12-06 18:21:12 +0000317 void addCCOutOperands(MCInst &Inst, unsigned N) const {
318 assert(N == 1 && "Invalid number of operands!");
319 Inst.addOperand(MCOperand::CreateReg(getReg()));
320 }
321
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000322 void addRegOperands(MCInst &Inst, unsigned N) const {
323 assert(N == 1 && "Invalid number of operands!");
324 Inst.addOperand(MCOperand::CreateReg(getReg()));
325 }
326
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000327 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000328 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000329 const SmallVectorImpl<unsigned> &RegList = getRegList();
330 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000331 I = RegList.begin(), E = RegList.end(); I != E; ++I)
332 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000333 }
334
Bill Wendling0f630752010-11-17 04:32:08 +0000335 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
336 addRegListOperands(Inst, N);
337 }
338
339 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
340 addRegListOperands(Inst, N);
341 }
342
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000343 void addImmOperands(MCInst &Inst, unsigned N) const {
344 assert(N == 1 && "Invalid number of operands!");
345 addExpr(Inst, getImm());
346 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000347
Chris Lattner14b93852010-10-29 00:27:31 +0000348 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
349 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000350
Daniel Dunbar4b462672011-01-18 05:55:27 +0000351 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
352 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000353
Jim Grosbach80eb2332010-10-29 17:41:25 +0000354 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
355 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000356 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000357 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000358
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000359 // The MCInst offset operand doesn't include the low two bits (like
360 // the instruction encoding).
361 int64_t Offset = CE->getValue() / 4;
362 if (Offset >= 0)
363 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
364 Offset)));
365 else
366 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
367 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000368 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000369
Bill Wendlingf4caf692010-12-14 03:36:38 +0000370 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
371 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000372 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
373 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000374 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000375
Bill Wendlingf4caf692010-12-14 03:36:38 +0000376 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
377 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000378 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
379 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000380 assert(CE && "Non-constant mode offset operand!");
381 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000382 }
383
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000384 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000385
Chris Lattner3a697562010-10-28 17:20:03 +0000386 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
387 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000388 Op->CC.Val = CC;
389 Op->StartLoc = S;
390 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000391 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000392 }
393
Jim Grosbachd67641b2010-12-06 18:21:12 +0000394 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
395 ARMOperand *Op = new ARMOperand(CCOut);
396 Op->Reg.RegNum = RegNum;
397 Op->StartLoc = S;
398 Op->EndLoc = S;
399 return Op;
400 }
401
Chris Lattner3a697562010-10-28 17:20:03 +0000402 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
403 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000404 Op->Tok.Data = Str.data();
405 Op->Tok.Length = Str.size();
406 Op->StartLoc = S;
407 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000408 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000409 }
410
Bill Wendling50d0f582010-11-18 23:43:05 +0000411 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000412 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000413 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000414 Op->StartLoc = S;
415 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000416 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000417 }
418
Bill Wendling7729e062010-11-09 22:44:22 +0000419 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000420 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000421 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000422 KindTy Kind = RegisterList;
423
424 if (ARM::DPRRegClass.contains(Regs.front().first))
425 Kind = DPRRegisterList;
426 else if (ARM::SPRRegClass.contains(Regs.front().first))
427 Kind = SPRRegisterList;
428
429 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000430 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000431 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000432 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000433 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000434 Op->StartLoc = StartLoc;
435 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000436 return Op;
437 }
438
Chris Lattner3a697562010-10-28 17:20:03 +0000439 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
440 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000441 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000442 Op->StartLoc = S;
443 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000444 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000445 }
446
Chris Lattner3a697562010-10-28 17:20:03 +0000447 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000448 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000449 bool OffsetRegShifted, enum ShiftType ShiftType,
450 const MCExpr *ShiftAmount, bool Preindexed,
451 bool Postindexed, bool Negative, bool Writeback,
452 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000453 assert((OffsetRegNum == -1 || OffsetIsReg) &&
454 "OffsetRegNum must imply OffsetIsReg!");
455 assert((!OffsetRegShifted || OffsetIsReg) &&
456 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000457 assert((Offset || OffsetIsReg) &&
458 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000459 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
460 "Cannot have shift amount without shifted register offset!");
461 assert((!Offset || !OffsetIsReg) &&
462 "Cannot have expression offset and register offset!");
463
Chris Lattner3a697562010-10-28 17:20:03 +0000464 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000465 Op->Mem.BaseRegNum = BaseRegNum;
466 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000467 if (OffsetIsReg)
468 Op->Mem.Offset.RegNum = OffsetRegNum;
469 else
470 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000471 Op->Mem.OffsetRegShifted = OffsetRegShifted;
472 Op->Mem.ShiftType = ShiftType;
473 Op->Mem.ShiftAmount = ShiftAmount;
474 Op->Mem.Preindexed = Preindexed;
475 Op->Mem.Postindexed = Postindexed;
476 Op->Mem.Negative = Negative;
477 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000478
Sean Callanan76264762010-04-02 22:27:05 +0000479 Op->StartLoc = S;
480 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000481 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000482 }
483};
484
485} // end anonymous namespace.
486
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000487void ARMOperand::dump(raw_ostream &OS) const {
488 switch (Kind) {
489 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000490 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000491 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000492 case CCOut:
493 OS << "<ccout " << getReg() << ">";
494 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000495 case Immediate:
496 getImm()->print(OS);
497 break;
498 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000499 OS << "<memory "
500 << "base:" << getMemBaseRegNum();
501 if (getMemOffsetIsReg()) {
502 OS << " offset:<register " << getMemOffsetRegNum();
503 if (getMemOffsetRegShifted()) {
504 OS << " offset-shift-type:" << getMemShiftType();
505 OS << " offset-shift-amount:" << *getMemShiftAmount();
506 }
507 } else {
508 OS << " offset:" << *getMemOffset();
509 }
510 if (getMemOffsetIsReg())
511 OS << " (offset-is-reg)";
512 if (getMemPreindexed())
513 OS << " (pre-indexed)";
514 if (getMemPostindexed())
515 OS << " (post-indexed)";
516 if (getMemNegative())
517 OS << " (negative)";
518 if (getMemWriteback())
519 OS << " (writeback)";
520 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000521 break;
522 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000523 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000524 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000525 case RegisterList:
526 case DPRRegisterList:
527 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000528 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000529
Bill Wendling5fa22a12010-11-09 23:28:44 +0000530 const SmallVectorImpl<unsigned> &RegList = getRegList();
531 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000532 I = RegList.begin(), E = RegList.end(); I != E; ) {
533 OS << *I;
534 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000535 }
536
537 OS << ">";
538 break;
539 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000540 case Token:
541 OS << "'" << getToken() << "'";
542 break;
543 }
544}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000545
546/// @name Auto-generated Match Functions
547/// {
548
549static unsigned MatchRegisterName(StringRef Name);
550
551/// }
552
Bob Wilson69df7232011-02-03 21:46:10 +0000553bool ARMAsmParser::ParseRegister(unsigned &RegNo,
554 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000555 RegNo = TryParseRegister();
556
557 return (RegNo == (unsigned)-1);
558}
559
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000560/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000561/// and if it is a register name the token is eaten and the register number is
562/// returned. Otherwise return -1.
563///
564int ARMAsmParser::TryParseRegister() {
565 const AsmToken &Tok = Parser.getTok();
566 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000567
Chris Lattnere5658fa2010-10-30 04:09:10 +0000568 // FIXME: Validate register for the current architecture; we have to do
569 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000570 std::string upperCase = Tok.getString().str();
571 std::string lowerCase = LowercaseString(upperCase);
572 unsigned RegNum = MatchRegisterName(lowerCase);
573 if (!RegNum) {
574 RegNum = StringSwitch<unsigned>(lowerCase)
575 .Case("r13", ARM::SP)
576 .Case("r14", ARM::LR)
577 .Case("r15", ARM::PC)
578 .Case("ip", ARM::R12)
579 .Default(0);
580 }
581 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000582
Chris Lattnere5658fa2010-10-30 04:09:10 +0000583 Parser.Lex(); // Eat identifier token.
584 return RegNum;
585}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000586
Bill Wendling50d0f582010-11-18 23:43:05 +0000587/// Try to parse a register name. The token must be an Identifier when called.
588/// If it's a register, an AsmOperand is created. Another AsmOperand is created
589/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000590///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000591/// TODO this is likely to change to allow different register types and or to
592/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000593bool ARMAsmParser::
594TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000595 SMLoc S = Parser.getTok().getLoc();
596 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000597 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000598 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000599
Bill Wendling50d0f582010-11-18 23:43:05 +0000600 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000601
Chris Lattnere5658fa2010-10-30 04:09:10 +0000602 const AsmToken &ExclaimTok = Parser.getTok();
603 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000604 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
605 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000606 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000607 }
608
Bill Wendling50d0f582010-11-18 23:43:05 +0000609 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000610}
611
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000612static int MatchCoprocessorOperandName(StringRef Name) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000613 // Use the same layout as the tablegen'erated register name matcher. Ugly,
614 // but efficient.
615 switch (Name.size()) {
616 default: break;
617 case 2:
618 if (Name[0] != 'p' && Name[0] != 'c')
619 return -1;
620 switch (Name[1]) {
621 default: return -1;
622 case '0': return 0;
623 case '1': return 1;
624 case '2': return 2;
625 case '3': return 3;
626 case '4': return 4;
627 case '5': return 5;
628 case '6': return 6;
629 case '7': return 7;
630 case '8': return 8;
631 case '9': return 9;
632 }
633 break;
634 case 3:
635 if ((Name[0] != 'p' && Name[0] != 'c') || Name[1] != '1')
636 return -1;
637 switch (Name[2]) {
638 default: return -1;
639 case '0': return 10;
640 case '1': return 11;
641 case '2': return 12;
642 case '3': return 13;
643 case '4': return 14;
644 case '5': return 15;
645 }
646 break;
647 }
648
649 llvm_unreachable("Unhandled coprocessor operand string!");
650 return -1;
651}
652
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000653/// TryParseCoprocessorOperandName - Try to parse an coprocessor related
654/// instruction with a symbolic operand name. The token must be an Identifier
655/// when called, and if it is a coprocessor related operand name, the token is
656/// eaten and the operand is added to the operand list. Example: operands like
657/// "p1", "p7", "c3", "c5", ...
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000658bool ARMAsmParser::
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000659TryParseCoprocessorOperandName(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000660 SMLoc S = Parser.getTok().getLoc();
661 const AsmToken &Tok = Parser.getTok();
662 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
663
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000664 int Num = MatchCoprocessorOperandName(Tok.getString());
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000665 if (Num == -1)
666 return true;
667
668 Parser.Lex(); // Eat identifier token.
669 Operands.push_back(ARMOperand::CreateImm(
670 MCConstantExpr::Create(Num, getContext()), S, Parser.getTok().getLoc()));
671 return false;
672}
673
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000674/// Parse a register list, return it if successful else return null. The first
675/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000676bool ARMAsmParser::
677ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000678 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000679 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000680 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000681
Bill Wendling7729e062010-11-09 22:44:22 +0000682 // Read the rest of the registers in the list.
683 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000684 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000685
Bill Wendling7729e062010-11-09 22:44:22 +0000686 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000687 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000688 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000689
Sean Callanan18b83232010-01-19 21:44:56 +0000690 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000691 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000692 if (RegTok.isNot(AsmToken::Identifier)) {
693 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000694 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000695 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000696
Bill Wendling1d6a2652010-11-06 10:40:24 +0000697 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000698 if (RegNum == -1) {
699 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000700 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000701 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000702
Bill Wendlinge7176102010-11-06 22:36:58 +0000703 if (IsRange) {
704 int Reg = PrevRegNum;
705 do {
706 ++Reg;
707 Registers.push_back(std::make_pair(Reg, RegLoc));
708 } while (Reg != RegNum);
709 } else {
710 Registers.push_back(std::make_pair(RegNum, RegLoc));
711 }
712
713 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000714 } while (Parser.getTok().is(AsmToken::Comma) ||
715 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000716
717 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000718 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000719 if (RCurlyTok.isNot(AsmToken::RCurly)) {
720 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000721 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000722 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000723
Bill Wendlinge7176102010-11-06 22:36:58 +0000724 SMLoc E = RCurlyTok.getLoc();
725 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000726
Bill Wendlinge7176102010-11-06 22:36:58 +0000727 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000728 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000729 RI = Registers.begin(), RE = Registers.end();
730
Bill Wendling7caebff2011-01-12 21:20:59 +0000731 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000732 bool EmittedWarning = false;
733
Bill Wendling7caebff2011-01-12 21:20:59 +0000734 DenseMap<unsigned, bool> RegMap;
735 RegMap[HighRegNum] = true;
736
Bill Wendlinge7176102010-11-06 22:36:58 +0000737 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000738 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000739 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000740
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000741 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000742 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000743 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000744 }
745
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000746 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000747 Warning(RegInfo.second,
748 "register not in ascending order in register list");
749
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000750 RegMap[Reg] = true;
751 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000752 }
753
Bill Wendling50d0f582010-11-18 23:43:05 +0000754 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
755 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000756}
757
Bill Wendlinge7176102010-11-06 22:36:58 +0000758/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000759/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000760///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000761/// TODO Only preindexing and postindexing addressing are started, unindexed
762/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000763bool ARMAsmParser::
764ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000765 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000766 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000767 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000768 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000769 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000770
Sean Callanan18b83232010-01-19 21:44:56 +0000771 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000772 if (BaseRegTok.isNot(AsmToken::Identifier)) {
773 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000774 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000775 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000776 int BaseRegNum = TryParseRegister();
777 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000778 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000779 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000780 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000781
Daniel Dunbar05710932011-01-18 05:34:17 +0000782 // The next token must either be a comma or a closing bracket.
783 const AsmToken &Tok = Parser.getTok();
784 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
785 return true;
786
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000787 bool Preindexed = false;
788 bool Postindexed = false;
789 bool OffsetIsReg = false;
790 bool Negative = false;
791 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000792 ARMOperand *WBOp = 0;
793 int OffsetRegNum = -1;
794 bool OffsetRegShifted = false;
795 enum ShiftType ShiftType = Lsl;
796 const MCExpr *ShiftAmount = 0;
797 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000798
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000799 // First look for preindexed address forms, that is after the "[Rn" we now
800 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000801 if (Tok.is(AsmToken::Comma)) {
802 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000803 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000804
Chris Lattner550276e2010-10-28 20:52:15 +0000805 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
806 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000807 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000808 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000809 if (RBracTok.isNot(AsmToken::RBrac)) {
810 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000811 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000812 }
Sean Callanan76264762010-04-02 22:27:05 +0000813 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000814 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000815
Sean Callanan18b83232010-01-19 21:44:56 +0000816 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000817 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000818 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
819 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000820 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000821 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000822 }
Daniel Dunbar05710932011-01-18 05:34:17 +0000823 } else {
824 // The "[Rn" we have so far was not followed by a comma.
825
Jim Grosbach80eb2332010-10-29 17:41:25 +0000826 // If there's anything other than the right brace, this is a post indexing
827 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000828 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000829 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000830
Sean Callanan18b83232010-01-19 21:44:56 +0000831 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000832
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000833 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000834 Postindexed = true;
835 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000836
Chris Lattner550276e2010-10-28 20:52:15 +0000837 if (NextTok.isNot(AsmToken::Comma)) {
838 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000839 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000840 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000841
Sean Callananb9a25b72010-01-19 20:27:46 +0000842 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000843
Chris Lattner550276e2010-10-28 20:52:15 +0000844 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000845 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000846 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000847 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000848 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000849 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000850
851 // Force Offset to exist if used.
852 if (!OffsetIsReg) {
853 if (!Offset)
854 Offset = MCConstantExpr::Create(0, getContext());
855 }
856
857 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
858 OffsetRegNum, OffsetRegShifted,
859 ShiftType, ShiftAmount, Preindexed,
860 Postindexed, Negative, Writeback,
861 S, E));
862 if (WBOp)
863 Operands.push_back(WBOp);
864
865 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000866}
867
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000868/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
869/// we will parse the following (were +/- means that a plus or minus is
870/// optional):
871/// +/-Rm
872/// +/-Rm, shift
873/// #offset
874/// we return false on success or an error otherwise.
875bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000876 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000877 enum ShiftType &ShiftType,
878 const MCExpr *&ShiftAmount,
879 const MCExpr *&Offset,
880 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000881 int &OffsetRegNum,
882 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000883 Negative = false;
884 OffsetRegShifted = false;
885 OffsetIsReg = false;
886 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000887 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000888 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000889 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000890 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000891 else if (NextTok.is(AsmToken::Minus)) {
892 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000893 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000894 }
895 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000896 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000897 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000898 SMLoc CurLoc = OffsetRegTok.getLoc();
899 OffsetRegNum = TryParseRegister();
900 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000901 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000902 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000903 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000904 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000905
Bill Wendling12f40e92010-11-06 10:51:53 +0000906 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000907 if (OffsetRegNum != -1) {
908 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000909 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000910 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000911 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000912
Sean Callanan18b83232010-01-19 21:44:56 +0000913 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000914 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000915 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000916 OffsetRegShifted = true;
917 }
918 }
919 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
920 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000921 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000922 if (HashTok.isNot(AsmToken::Hash))
923 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000924
Sean Callananb9a25b72010-01-19 20:27:46 +0000925 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000926
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000927 if (getParser().ParseExpression(Offset))
928 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000929 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000930 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000931 return false;
932}
933
934/// ParseShift as one of these two:
935/// ( lsl | lsr | asr | ror ) , # shift_amount
936/// rrx
937/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000938bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000939 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000940 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000941 if (Tok.isNot(AsmToken::Identifier))
942 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000943 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000944 if (ShiftName == "lsl" || ShiftName == "LSL")
945 St = Lsl;
946 else if (ShiftName == "lsr" || ShiftName == "LSR")
947 St = Lsr;
948 else if (ShiftName == "asr" || ShiftName == "ASR")
949 St = Asr;
950 else if (ShiftName == "ror" || ShiftName == "ROR")
951 St = Ror;
952 else if (ShiftName == "rrx" || ShiftName == "RRX")
953 St = Rrx;
954 else
955 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000956 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000957
958 // Rrx stands alone.
959 if (St == Rrx)
960 return false;
961
962 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000963 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000964 if (HashTok.isNot(AsmToken::Hash))
965 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000966 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000967
968 if (getParser().ParseExpression(ShiftAmount))
969 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000970
971 return false;
972}
973
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000974/// Parse a arm instruction operand. For now this parses the operand regardless
975/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000976bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000977 bool hasCoprocOp){
Sean Callanan76264762010-04-02 22:27:05 +0000978 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000979 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000980 default:
981 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000982 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +0000983 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +0000984 if (!TryParseRegisterWithWriteBack(Operands))
985 return false;
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000986 if (hasCoprocOp && !TryParseCoprocessorOperandName(Operands))
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000987 return false;
988
989 // Fall though for the Identifier case that is not a register or a
990 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +0000991 case AsmToken::Integer: // things like 1f and 2b as a branch targets
992 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +0000993 // This was not a register so parse other operands that start with an
994 // identifier (like labels) as expressions and create them as immediates.
995 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000996 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000997 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000998 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000999 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001000 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1001 return false;
1002 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001003 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001004 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001005 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001006 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001007 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001008 // #42 -> immediate.
1009 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001010 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001011 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001012 const MCExpr *ImmVal;
1013 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001014 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001015 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001016 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1017 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001018 case AsmToken::Colon: {
1019 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001020 // FIXME: Check it's an expression prefix,
1021 // e.g. (FOO - :lower16:BAR) isn't legal.
1022 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001023 if (ParsePrefix(RefKind))
1024 return true;
1025
Evan Cheng75972122011-01-13 07:58:56 +00001026 const MCExpr *SubExprVal;
1027 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001028 return true;
1029
Evan Cheng75972122011-01-13 07:58:56 +00001030 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1031 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001032 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001033 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001034 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001035 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001036 }
1037}
1038
Evan Cheng75972122011-01-13 07:58:56 +00001039// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1040// :lower16: and :upper16:.
1041bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1042 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001043
1044 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001045 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001046 Parser.Lex(); // Eat ':'
1047
1048 if (getLexer().isNot(AsmToken::Identifier)) {
1049 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1050 return true;
1051 }
1052
1053 StringRef IDVal = Parser.getTok().getIdentifier();
1054 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001055 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001056 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001057 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001058 } else {
1059 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1060 return true;
1061 }
1062 Parser.Lex();
1063
1064 if (getLexer().isNot(AsmToken::Colon)) {
1065 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1066 return true;
1067 }
1068 Parser.Lex(); // Eat the last ':'
1069 return false;
1070}
1071
1072const MCExpr *
1073ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1074 MCSymbolRefExpr::VariantKind Variant) {
1075 // Recurse over the given expression, rebuilding it to apply the given variant
1076 // to the leftmost symbol.
1077 if (Variant == MCSymbolRefExpr::VK_None)
1078 return E;
1079
1080 switch (E->getKind()) {
1081 case MCExpr::Target:
1082 llvm_unreachable("Can't handle target expr yet");
1083 case MCExpr::Constant:
1084 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1085
1086 case MCExpr::SymbolRef: {
1087 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1088
1089 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1090 return 0;
1091
1092 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1093 }
1094
1095 case MCExpr::Unary:
1096 llvm_unreachable("Can't handle unary expressions yet");
1097
1098 case MCExpr::Binary: {
1099 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1100 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1101 const MCExpr *RHS = BE->getRHS();
1102 if (!LHS)
1103 return 0;
1104
1105 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1106 }
1107 }
1108
1109 assert(0 && "Invalid expression kind!");
1110 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001111}
1112
Daniel Dunbar352e1482011-01-11 15:59:50 +00001113/// \brief Given a mnemonic, split out possible predication code and carry
1114/// setting letters to form a canonical mnemonic and flags.
1115//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001116// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001117static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
1118 unsigned &PredicationCode,
1119 bool &CarrySetting) {
1120 PredicationCode = ARMCC::AL;
1121 CarrySetting = false;
1122
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001123 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001124 //
1125 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001126 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1127 Mnemonic == "movs" ||
1128 Mnemonic == "svc" ||
1129 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1130 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1131 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1132 Mnemonic == "vclt" ||
1133 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1134 Mnemonic == "vcle" ||
1135 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1136 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1137 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001138 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001139
Daniel Dunbar352e1482011-01-11 15:59:50 +00001140 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001141 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001142 .Case("eq", ARMCC::EQ)
1143 .Case("ne", ARMCC::NE)
1144 .Case("hs", ARMCC::HS)
1145 .Case("lo", ARMCC::LO)
1146 .Case("mi", ARMCC::MI)
1147 .Case("pl", ARMCC::PL)
1148 .Case("vs", ARMCC::VS)
1149 .Case("vc", ARMCC::VC)
1150 .Case("hi", ARMCC::HI)
1151 .Case("ls", ARMCC::LS)
1152 .Case("ge", ARMCC::GE)
1153 .Case("lt", ARMCC::LT)
1154 .Case("gt", ARMCC::GT)
1155 .Case("le", ARMCC::LE)
1156 .Case("al", ARMCC::AL)
1157 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001158 if (CC != ~0U) {
1159 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001160 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001161 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001162
Daniel Dunbar352e1482011-01-11 15:59:50 +00001163 // Next, determine if we have a carry setting bit. We explicitly ignore all
1164 // the instructions we know end in 's'.
1165 if (Mnemonic.endswith("s") &&
1166 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1167 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1168 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1169 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1170 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1171 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1172 CarrySetting = true;
1173 }
1174
1175 return Mnemonic;
1176}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001177
1178/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1179/// inclusion of carry set or predication code operands.
1180//
1181// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001182void ARMAsmParser::
1183GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1184 bool &CanAcceptPredicationCode) {
1185 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1186
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001187 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1188 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1189 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1190 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1191 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1192 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1193 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1194 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1195 CanAcceptCarrySet = true;
1196 } else {
1197 CanAcceptCarrySet = false;
1198 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001199
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001200 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1201 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1202 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1203 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001204 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
1205 Mnemonic == "clrex") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001206 CanAcceptPredicationCode = false;
1207 } else {
1208 CanAcceptPredicationCode = true;
1209 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001210
1211 if (isThumb)
1212 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001213 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001214 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001215}
1216
1217/// Parse an arm instruction mnemonic followed by its operands.
1218bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1219 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1220 // Create the leading tokens for the mnemonic, split by '.' characters.
1221 size_t Start = 0, Next = Name.find('.');
1222 StringRef Head = Name.slice(Start, Next);
1223
Daniel Dunbar352e1482011-01-11 15:59:50 +00001224 // Split out the predication code and carry setting flag from the mnemonic.
1225 unsigned PredicationCode;
1226 bool CarrySetting;
1227 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001228
Chris Lattner3a697562010-10-28 17:20:03 +00001229 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001230
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001231 // Next, add the CCOut and ConditionCode operands, if needed.
1232 //
1233 // For mnemonics which can ever incorporate a carry setting bit or predication
1234 // code, our matching model involves us always generating CCOut and
1235 // ConditionCode operands to match the mnemonic "as written" and then we let
1236 // the matcher deal with finding the right instruction or generating an
1237 // appropriate error.
1238 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1239 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1240
1241 // Add the carry setting operand, if necessary.
1242 //
1243 // FIXME: It would be awesome if we could somehow invent a location such that
1244 // match errors on this operand would print a nice diagnostic about how the
1245 // 's' character in the mnemonic resulted in a CCOut operand.
1246 if (CanAcceptCarrySet) {
1247 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1248 NameLoc));
1249 } else {
1250 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1251 // misspelled another mnemonic).
1252
1253 // FIXME: Issue a nice error.
1254 }
1255
1256 // Add the predication code operand, if necessary.
1257 if (CanAcceptPredicationCode) {
1258 Operands.push_back(ARMOperand::CreateCondCode(
1259 ARMCC::CondCodes(PredicationCode), NameLoc));
1260 } else {
1261 // This mnemonic can't ever accept a predication code, but the user wrote
1262 // one (or misspelled another mnemonic).
1263
1264 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001265 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001266
1267 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001268 while (Next != StringRef::npos) {
1269 Start = Next;
1270 Next = Name.find('.', Start + 1);
1271 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001272
Chris Lattner3a697562010-10-28 17:20:03 +00001273 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001274 }
1275
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +00001276 // Enable the parsing of instructions containing coprocessor related
1277 // asm syntax, such as coprocessor names "p7, p15, ..." and coprocessor
1278 // registers "c1, c3, ..."
1279 // FIXME: we probably want AsmOperandClass and ParserMatchClass declarations
1280 // in the .td file rather than hacking the ASMParser for every symbolic
1281 // operand type.
1282 bool hasCoprocOp = (Head == "mcr" || Head == "mcr2" ||
1283 Head == "mcrr" || Head == "mcrr2" ||
1284 Head == "mrc" || Head == "mrc2" ||
1285 Head == "mrrc" || Head == "mrrc2" ||
1286 Head == "cdp" || Head == "cdp2");
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001287
Daniel Dunbar5747b132010-08-11 06:37:16 +00001288 // Read the remaining operands.
1289 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001290 // Read the first operand.
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +00001291 if (ParseOperand(Operands, hasCoprocOp)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001292 Parser.EatToEndOfStatement();
1293 return true;
1294 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001295
1296 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001297 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001298
1299 // Parse and remember the operand.
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +00001300 if (ParseOperand(Operands, hasCoprocOp)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001301 Parser.EatToEndOfStatement();
1302 return true;
1303 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001304 }
1305 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001306
Chris Lattnercbf8a982010-09-11 16:18:25 +00001307 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1308 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001309 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001310 }
Bill Wendling146018f2010-11-06 21:42:12 +00001311
Chris Lattner34e53142010-09-08 05:10:46 +00001312 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001313 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001314}
1315
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001316bool ARMAsmParser::
1317MatchAndEmitInstruction(SMLoc IDLoc,
1318 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1319 MCStreamer &Out) {
1320 MCInst Inst;
1321 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001322 MatchResultTy MatchResult, MatchResult2;
1323 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1324 if (MatchResult != Match_Success) {
1325 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1326 // that does not update the condition codes. So try adding a CCOut operand
1327 // with a value of reg0.
1328 if (MatchResult == Match_InvalidOperand) {
1329 Operands.insert(Operands.begin() + 1,
1330 ARMOperand::CreateCCOut(0,
1331 ((ARMOperand*)Operands[0])->getStartLoc()));
1332 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1333 if (MatchResult2 == Match_Success)
1334 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001335 else {
1336 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001337 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001338 delete CCOut;
1339 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001340 }
1341 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1342 // that updates the condition codes if it ends in 's'. So see if the
1343 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1344 // operand with a value of CPSR.
1345 else if(MatchResult == Match_MnemonicFail) {
1346 // Get the instruction mnemonic, which is the first token.
1347 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1348 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1349 // removed the 's' from the mnemonic for matching.
1350 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1351 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001352 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1353 Operands.erase(Operands.begin());
1354 delete OldMnemonic;
1355 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001356 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1357 Operands.insert(Operands.begin() + 1,
1358 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1359 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1360 if (MatchResult2 == Match_Success)
1361 MatchResult = Match_Success;
1362 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001363 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1364 Operands.erase(Operands.begin());
1365 delete OldMnemonic;
1366 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001367 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001368 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1369 Operands.erase(Operands.begin() + 1);
1370 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001371 }
1372 }
1373 }
1374 }
1375 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001376 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001377 Out.EmitInstruction(Inst);
1378 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001379 case Match_MissingFeature:
1380 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1381 return true;
1382 case Match_InvalidOperand: {
1383 SMLoc ErrorLoc = IDLoc;
1384 if (ErrorInfo != ~0U) {
1385 if (ErrorInfo >= Operands.size())
1386 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001387
Chris Lattnere73d4f82010-10-28 21:41:58 +00001388 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1389 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1390 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001391
Chris Lattnere73d4f82010-10-28 21:41:58 +00001392 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001393 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001394 case Match_MnemonicFail:
1395 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00001396 case Match_ConversionFail:
1397 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00001398 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001399
Eric Christopherc223e2b2010-10-29 09:26:59 +00001400 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001401 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001402}
1403
Kevin Enderby515d5092009-10-15 20:48:48 +00001404/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001405bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1406 StringRef IDVal = DirectiveID.getIdentifier();
1407 if (IDVal == ".word")
1408 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001409 else if (IDVal == ".thumb")
1410 return ParseDirectiveThumb(DirectiveID.getLoc());
1411 else if (IDVal == ".thumb_func")
1412 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1413 else if (IDVal == ".code")
1414 return ParseDirectiveCode(DirectiveID.getLoc());
1415 else if (IDVal == ".syntax")
1416 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001417 return true;
1418}
1419
1420/// ParseDirectiveWord
1421/// ::= .word [ expression (, expression)* ]
1422bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1423 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1424 for (;;) {
1425 const MCExpr *Value;
1426 if (getParser().ParseExpression(Value))
1427 return true;
1428
Chris Lattneraaec2052010-01-19 19:46:13 +00001429 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001430
1431 if (getLexer().is(AsmToken::EndOfStatement))
1432 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001433
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001434 // FIXME: Improve diagnostic.
1435 if (getLexer().isNot(AsmToken::Comma))
1436 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001437 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001438 }
1439 }
1440
Sean Callananb9a25b72010-01-19 20:27:46 +00001441 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001442 return false;
1443}
1444
Kevin Enderby515d5092009-10-15 20:48:48 +00001445/// ParseDirectiveThumb
1446/// ::= .thumb
1447bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1448 if (getLexer().isNot(AsmToken::EndOfStatement))
1449 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001450 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001451
1452 // TODO: set thumb mode
1453 // TODO: tell the MC streamer the mode
1454 // getParser().getStreamer().Emit???();
1455 return false;
1456}
1457
1458/// ParseDirectiveThumbFunc
1459/// ::= .thumbfunc symbol_name
1460bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001461 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001462 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001463 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001464 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001465 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001466 if (getLexer().isNot(AsmToken::EndOfStatement))
1467 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001468 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001469
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001470 // Mark symbol as a thumb symbol.
1471 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1472 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001473 return false;
1474}
1475
1476/// ParseDirectiveSyntax
1477/// ::= .syntax unified | divided
1478bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001479 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001480 if (Tok.isNot(AsmToken::Identifier))
1481 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001482 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001483 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001484 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001485 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00001486 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00001487 else
1488 return Error(L, "unrecognized syntax mode in .syntax directive");
1489
1490 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001491 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001492 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001493
1494 // TODO tell the MC streamer the mode
1495 // getParser().getStreamer().Emit???();
1496 return false;
1497}
1498
1499/// ParseDirectiveCode
1500/// ::= .code 16 | 32
1501bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001502 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001503 if (Tok.isNot(AsmToken::Integer))
1504 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001505 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001506 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001507 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001508 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001509 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001510 else
1511 return Error(L, "invalid operand to .code directive");
1512
1513 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001514 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001515 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001516
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001517 // FIXME: We need to be able switch subtargets at this point so that
1518 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1519 // includes Feature_IsThumb or not to match the right instructions. This is
1520 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1521 if (Val == 16){
1522 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1523 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001524 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001525 }
1526 else{
1527 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1528 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001529 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001530 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001531
Kevin Enderby515d5092009-10-15 20:48:48 +00001532 return false;
1533}
1534
Sean Callanan90b70972010-04-07 20:29:34 +00001535extern "C" void LLVMInitializeARMAsmLexer();
1536
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001537/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001538extern "C" void LLVMInitializeARMAsmParser() {
1539 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1540 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001541 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001542}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001543
Chris Lattner0692ee62010-09-06 19:11:01 +00001544#define GET_REGISTER_MATCHER
1545#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001546#include "ARMGenAsmMatcher.inc"