blob: a10e1581050d63e58807d388f5f363688972458b [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();
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +000056 bool TryParseCoprocessorOperandName(SmallVectorImpl<MCParsedAsmOperand*>&);
Bill Wendling50d0f582010-11-18 23:43:05 +000057 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
58 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
59 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +000060 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, bool hasCoprocOp);
Evan Cheng75972122011-01-13 07:58:56 +000061 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000062 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
63 MCSymbolRefExpr::VariantKind Variant);
64
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000065
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 bool ParseMemoryOffsetReg(bool &Negative,
67 bool &OffsetRegShifted,
68 enum ShiftType &ShiftType,
69 const MCExpr *&ShiftAmount,
70 const MCExpr *&Offset,
71 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000072 int &OffsetRegNum,
73 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000074 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000075 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000076 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000078 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveSyntax(SMLoc L);
80
Chris Lattner7036f8b2010-09-29 01:42:58 +000081 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000082 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000083 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000084 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
85 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000086
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000087 /// @name Auto-generated Match Functions
88 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000089
Chris Lattner0692ee62010-09-06 19:11:01 +000090#define GET_ASSEMBLER_HEADER
91#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000092
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000093 /// }
94
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000095public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000096 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000097 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
98 // Initialize the set of available features.
99 setAvailableFeatures(ComputeAvailableFeatures(
100 &TM.getSubtarget<ARMSubtarget>()));
101 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000102
Benjamin Kramer38e59892010-07-14 22:38:02 +0000103 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000104 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000105 virtual bool ParseDirective(AsmToken DirectiveID);
106};
Jim Grosbach16c74252010-10-29 14:46:02 +0000107} // end anonymous namespace
108
Chris Lattner3a697562010-10-28 17:20:03 +0000109namespace {
110
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000111/// ARMOperand - Instances of this class represent a parsed ARM machine
112/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000113class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000114 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000115 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000116 CCOut,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000117 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000118 Memory,
119 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000120 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000121 DPRRegisterList,
122 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000123 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000124 } Kind;
125
Sean Callanan76264762010-04-02 22:27:05 +0000126 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000127 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000128
129 union {
130 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000131 ARMCC::CondCodes Val;
132 } CC;
133
134 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000135 const char *Data;
136 unsigned Length;
137 } Tok;
138
139 struct {
140 unsigned RegNum;
141 } Reg;
142
Bill Wendling8155e5b2010-11-06 22:19:43 +0000143 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000144 const MCExpr *Val;
145 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000146
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000147 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000148 struct {
149 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000150 union {
151 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
152 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
153 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000154 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
155 enum ShiftType ShiftType; // used when OffsetRegShifted is true
156 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000157 unsigned Preindexed : 1;
158 unsigned Postindexed : 1;
159 unsigned OffsetIsReg : 1;
160 unsigned Negative : 1; // only used when OffsetIsReg is true
161 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000162 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000163 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000164
Bill Wendling146018f2010-11-06 21:42:12 +0000165 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
166public:
Sean Callanan76264762010-04-02 22:27:05 +0000167 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
168 Kind = o.Kind;
169 StartLoc = o.StartLoc;
170 EndLoc = o.EndLoc;
171 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000172 case CondCode:
173 CC = o.CC;
174 break;
Sean Callanan76264762010-04-02 22:27:05 +0000175 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000176 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000177 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000178 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000179 case Register:
180 Reg = o.Reg;
181 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000182 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000183 case DPRRegisterList:
184 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000185 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000186 break;
Sean Callanan76264762010-04-02 22:27:05 +0000187 case Immediate:
188 Imm = o.Imm;
189 break;
190 case Memory:
191 Mem = o.Mem;
192 break;
193 }
194 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000195
Sean Callanan76264762010-04-02 22:27:05 +0000196 /// getStartLoc - Get the location of the first token of this operand.
197 SMLoc getStartLoc() const { return StartLoc; }
198 /// getEndLoc - Get the location of the last token of this operand.
199 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000200
Daniel Dunbar8462b302010-08-11 06:36:53 +0000201 ARMCC::CondCodes getCondCode() const {
202 assert(Kind == CondCode && "Invalid access!");
203 return CC.Val;
204 }
205
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000206 StringRef getToken() const {
207 assert(Kind == Token && "Invalid access!");
208 return StringRef(Tok.Data, Tok.Length);
209 }
210
211 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000212 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000213 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000214 }
215
Bill Wendling5fa22a12010-11-09 23:28:44 +0000216 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000217 assert((Kind == RegisterList || Kind == DPRRegisterList ||
218 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000219 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000220 }
221
Kevin Enderbycfe07242009-10-13 22:19:02 +0000222 const MCExpr *getImm() const {
223 assert(Kind == Immediate && "Invalid access!");
224 return Imm.Val;
225 }
226
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000227 /// @name Memory Operand Accessors
228 /// @{
229
230 unsigned getMemBaseRegNum() const {
231 return Mem.BaseRegNum;
232 }
233 unsigned getMemOffsetRegNum() const {
234 assert(Mem.OffsetIsReg && "Invalid access!");
235 return Mem.Offset.RegNum;
236 }
237 const MCExpr *getMemOffset() const {
238 assert(!Mem.OffsetIsReg && "Invalid access!");
239 return Mem.Offset.Value;
240 }
241 unsigned getMemOffsetRegShifted() const {
242 assert(Mem.OffsetIsReg && "Invalid access!");
243 return Mem.OffsetRegShifted;
244 }
245 const MCExpr *getMemShiftAmount() const {
246 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
247 return Mem.ShiftAmount;
248 }
249 enum ShiftType getMemShiftType() const {
250 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
251 return Mem.ShiftType;
252 }
253 bool getMemPreindexed() const { return Mem.Preindexed; }
254 bool getMemPostindexed() const { return Mem.Postindexed; }
255 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
256 bool getMemNegative() const { return Mem.Negative; }
257 bool getMemWriteback() const { return Mem.Writeback; }
258
259 /// @}
260
Daniel Dunbar8462b302010-08-11 06:36:53 +0000261 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000262 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000263 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000264 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000265 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000266 bool isDPRRegList() const { return Kind == DPRRegisterList; }
267 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000268 bool isToken() const { return Kind == Token; }
269 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000270 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000271 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
272 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000273 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000274
Daniel Dunbar4b462672011-01-18 05:55:27 +0000275 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000276 if (!CE) return false;
277
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000278 // The offset must be a multiple of 4 in the range 0-1020.
279 int64_t Value = CE->getValue();
280 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
281 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000282 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000283 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000284 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000285 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000286 }
287 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000288 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000289 return false;
290
Daniel Dunbar4b462672011-01-18 05:55:27 +0000291 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000292 if (!CE) return false;
293
294 // The offset must be a multiple of 4 in the range 0-124.
295 uint64_t Value = CE->getValue();
296 return ((Value & 0x3) == 0 && Value <= 124);
297 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000298
299 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000300 // Add as immediates when possible. Null MCExpr = 0.
301 if (Expr == 0)
302 Inst.addOperand(MCOperand::CreateImm(0));
303 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000304 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
305 else
306 Inst.addOperand(MCOperand::CreateExpr(Expr));
307 }
308
Daniel Dunbar8462b302010-08-11 06:36:53 +0000309 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000310 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000311 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000312 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
313 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000314 }
315
Jim Grosbachd67641b2010-12-06 18:21:12 +0000316 void addCCOutOperands(MCInst &Inst, unsigned N) const {
317 assert(N == 1 && "Invalid number of operands!");
318 Inst.addOperand(MCOperand::CreateReg(getReg()));
319 }
320
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000321 void addRegOperands(MCInst &Inst, unsigned N) const {
322 assert(N == 1 && "Invalid number of operands!");
323 Inst.addOperand(MCOperand::CreateReg(getReg()));
324 }
325
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000326 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000327 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000328 const SmallVectorImpl<unsigned> &RegList = getRegList();
329 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000330 I = RegList.begin(), E = RegList.end(); I != E; ++I)
331 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000332 }
333
Bill Wendling0f630752010-11-17 04:32:08 +0000334 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
335 addRegListOperands(Inst, N);
336 }
337
338 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
339 addRegListOperands(Inst, N);
340 }
341
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000342 void addImmOperands(MCInst &Inst, unsigned N) const {
343 assert(N == 1 && "Invalid number of operands!");
344 addExpr(Inst, getImm());
345 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000346
Chris Lattner14b93852010-10-29 00:27:31 +0000347 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
348 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000349
Daniel Dunbar4b462672011-01-18 05:55:27 +0000350 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
351 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000352
Jim Grosbach80eb2332010-10-29 17:41:25 +0000353 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
354 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000355 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000356 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000357
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000358 // The MCInst offset operand doesn't include the low two bits (like
359 // the instruction encoding).
360 int64_t Offset = CE->getValue() / 4;
361 if (Offset >= 0)
362 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
363 Offset)));
364 else
365 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
366 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000367 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000368
Bill Wendlingf4caf692010-12-14 03:36:38 +0000369 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
370 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000371 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
372 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000373 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000374
Bill Wendlingf4caf692010-12-14 03:36:38 +0000375 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
376 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000377 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
378 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000379 assert(CE && "Non-constant mode offset operand!");
380 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000381 }
382
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000383 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000384
Chris Lattner3a697562010-10-28 17:20:03 +0000385 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
386 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000387 Op->CC.Val = CC;
388 Op->StartLoc = S;
389 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000390 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000391 }
392
Jim Grosbachd67641b2010-12-06 18:21:12 +0000393 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
394 ARMOperand *Op = new ARMOperand(CCOut);
395 Op->Reg.RegNum = RegNum;
396 Op->StartLoc = S;
397 Op->EndLoc = S;
398 return Op;
399 }
400
Chris Lattner3a697562010-10-28 17:20:03 +0000401 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
402 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000403 Op->Tok.Data = Str.data();
404 Op->Tok.Length = Str.size();
405 Op->StartLoc = S;
406 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000407 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000408 }
409
Bill Wendling50d0f582010-11-18 23:43:05 +0000410 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000411 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000412 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000413 Op->StartLoc = S;
414 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000415 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000416 }
417
Bill Wendling7729e062010-11-09 22:44:22 +0000418 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000419 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000420 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000421 KindTy Kind = RegisterList;
422
423 if (ARM::DPRRegClass.contains(Regs.front().first))
424 Kind = DPRRegisterList;
425 else if (ARM::SPRRegClass.contains(Regs.front().first))
426 Kind = SPRRegisterList;
427
428 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000429 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000430 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000431 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000432 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000433 Op->StartLoc = StartLoc;
434 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000435 return Op;
436 }
437
Chris Lattner3a697562010-10-28 17:20:03 +0000438 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
439 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000440 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000441 Op->StartLoc = S;
442 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000443 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000444 }
445
Chris Lattner3a697562010-10-28 17:20:03 +0000446 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000447 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000448 bool OffsetRegShifted, enum ShiftType ShiftType,
449 const MCExpr *ShiftAmount, bool Preindexed,
450 bool Postindexed, bool Negative, bool Writeback,
451 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000452 assert((OffsetRegNum == -1 || OffsetIsReg) &&
453 "OffsetRegNum must imply OffsetIsReg!");
454 assert((!OffsetRegShifted || OffsetIsReg) &&
455 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000456 assert((Offset || OffsetIsReg) &&
457 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000458 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
459 "Cannot have shift amount without shifted register offset!");
460 assert((!Offset || !OffsetIsReg) &&
461 "Cannot have expression offset and register offset!");
462
Chris Lattner3a697562010-10-28 17:20:03 +0000463 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000464 Op->Mem.BaseRegNum = BaseRegNum;
465 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000466 if (OffsetIsReg)
467 Op->Mem.Offset.RegNum = OffsetRegNum;
468 else
469 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000470 Op->Mem.OffsetRegShifted = OffsetRegShifted;
471 Op->Mem.ShiftType = ShiftType;
472 Op->Mem.ShiftAmount = ShiftAmount;
473 Op->Mem.Preindexed = Preindexed;
474 Op->Mem.Postindexed = Postindexed;
475 Op->Mem.Negative = Negative;
476 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000477
Sean Callanan76264762010-04-02 22:27:05 +0000478 Op->StartLoc = S;
479 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000480 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000481 }
482};
483
484} // end anonymous namespace.
485
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000486void ARMOperand::dump(raw_ostream &OS) const {
487 switch (Kind) {
488 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000489 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000490 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000491 case CCOut:
492 OS << "<ccout " << getReg() << ">";
493 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000494 case Immediate:
495 getImm()->print(OS);
496 break;
497 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000498 OS << "<memory "
499 << "base:" << getMemBaseRegNum();
500 if (getMemOffsetIsReg()) {
501 OS << " offset:<register " << getMemOffsetRegNum();
502 if (getMemOffsetRegShifted()) {
503 OS << " offset-shift-type:" << getMemShiftType();
504 OS << " offset-shift-amount:" << *getMemShiftAmount();
505 }
506 } else {
507 OS << " offset:" << *getMemOffset();
508 }
509 if (getMemOffsetIsReg())
510 OS << " (offset-is-reg)";
511 if (getMemPreindexed())
512 OS << " (pre-indexed)";
513 if (getMemPostindexed())
514 OS << " (post-indexed)";
515 if (getMemNegative())
516 OS << " (negative)";
517 if (getMemWriteback())
518 OS << " (writeback)";
519 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000520 break;
521 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000522 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000523 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000524 case RegisterList:
525 case DPRRegisterList:
526 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000527 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000528
Bill Wendling5fa22a12010-11-09 23:28:44 +0000529 const SmallVectorImpl<unsigned> &RegList = getRegList();
530 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000531 I = RegList.begin(), E = RegList.end(); I != E; ) {
532 OS << *I;
533 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000534 }
535
536 OS << ">";
537 break;
538 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000539 case Token:
540 OS << "'" << getToken() << "'";
541 break;
542 }
543}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000544
545/// @name Auto-generated Match Functions
546/// {
547
548static unsigned MatchRegisterName(StringRef Name);
549
550/// }
551
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000552/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000553/// and if it is a register name the token is eaten and the register number is
554/// returned. Otherwise return -1.
555///
556int ARMAsmParser::TryParseRegister() {
557 const AsmToken &Tok = Parser.getTok();
558 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000559
Chris Lattnere5658fa2010-10-30 04:09:10 +0000560 // FIXME: Validate register for the current architecture; we have to do
561 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000562 std::string upperCase = Tok.getString().str();
563 std::string lowerCase = LowercaseString(upperCase);
564 unsigned RegNum = MatchRegisterName(lowerCase);
565 if (!RegNum) {
566 RegNum = StringSwitch<unsigned>(lowerCase)
567 .Case("r13", ARM::SP)
568 .Case("r14", ARM::LR)
569 .Case("r15", ARM::PC)
570 .Case("ip", ARM::R12)
571 .Default(0);
572 }
573 if (!RegNum) return -1;
574
Chris Lattnere5658fa2010-10-30 04:09:10 +0000575 Parser.Lex(); // Eat identifier token.
576 return RegNum;
577}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000578
579
Bill Wendling50d0f582010-11-18 23:43:05 +0000580/// Try to parse a register name. The token must be an Identifier when called.
581/// If it's a register, an AsmOperand is created. Another AsmOperand is created
582/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000583///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000584/// TODO this is likely to change to allow different register types and or to
585/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000586bool ARMAsmParser::
587TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000588 SMLoc S = Parser.getTok().getLoc();
589 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000590 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000591 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000592
Bill Wendling50d0f582010-11-18 23:43:05 +0000593 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000594
Chris Lattnere5658fa2010-10-30 04:09:10 +0000595 const AsmToken &ExclaimTok = Parser.getTok();
596 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000597 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
598 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000599 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000600 }
601
Bill Wendling50d0f582010-11-18 23:43:05 +0000602 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000603}
604
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000605static int MatchCoprocessorOperandName(StringRef Name) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000606 // Use the same layout as the tablegen'erated register name matcher. Ugly,
607 // but efficient.
608 switch (Name.size()) {
609 default: break;
610 case 2:
611 if (Name[0] != 'p' && Name[0] != 'c')
612 return -1;
613 switch (Name[1]) {
614 default: return -1;
615 case '0': return 0;
616 case '1': return 1;
617 case '2': return 2;
618 case '3': return 3;
619 case '4': return 4;
620 case '5': return 5;
621 case '6': return 6;
622 case '7': return 7;
623 case '8': return 8;
624 case '9': return 9;
625 }
626 break;
627 case 3:
628 if ((Name[0] != 'p' && Name[0] != 'c') || Name[1] != '1')
629 return -1;
630 switch (Name[2]) {
631 default: return -1;
632 case '0': return 10;
633 case '1': return 11;
634 case '2': return 12;
635 case '3': return 13;
636 case '4': return 14;
637 case '5': return 15;
638 }
639 break;
640 }
641
642 llvm_unreachable("Unhandled coprocessor operand string!");
643 return -1;
644}
645
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000646/// TryParseCoprocessorOperandName - Try to parse an coprocessor related
647/// instruction with a symbolic operand name. The token must be an Identifier
648/// when called, and if it is a coprocessor related operand name, the token is
649/// eaten and the operand is added to the operand list. Example: operands like
650/// "p1", "p7", "c3", "c5", ...
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000651bool ARMAsmParser::
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000652TryParseCoprocessorOperandName(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000653 SMLoc S = Parser.getTok().getLoc();
654 const AsmToken &Tok = Parser.getTok();
655 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
656
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000657 int Num = MatchCoprocessorOperandName(Tok.getString());
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000658 if (Num == -1)
659 return true;
660
661 Parser.Lex(); // Eat identifier token.
662 Operands.push_back(ARMOperand::CreateImm(
663 MCConstantExpr::Create(Num, getContext()), S, Parser.getTok().getLoc()));
664 return false;
665}
666
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000667/// Parse a register list, return it if successful else return null. The first
668/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000669bool ARMAsmParser::
670ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000671 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000672 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000673 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000674
Bill Wendling7729e062010-11-09 22:44:22 +0000675 // Read the rest of the registers in the list.
676 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000677 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000678
Bill Wendling7729e062010-11-09 22:44:22 +0000679 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000680 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000681 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000682
Sean Callanan18b83232010-01-19 21:44:56 +0000683 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000684 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000685 if (RegTok.isNot(AsmToken::Identifier)) {
686 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000687 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000688 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000689
Bill Wendling1d6a2652010-11-06 10:40:24 +0000690 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000691 if (RegNum == -1) {
692 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000693 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000694 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000695
Bill Wendlinge7176102010-11-06 22:36:58 +0000696 if (IsRange) {
697 int Reg = PrevRegNum;
698 do {
699 ++Reg;
700 Registers.push_back(std::make_pair(Reg, RegLoc));
701 } while (Reg != RegNum);
702 } else {
703 Registers.push_back(std::make_pair(RegNum, RegLoc));
704 }
705
706 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000707 } while (Parser.getTok().is(AsmToken::Comma) ||
708 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000709
710 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000711 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000712 if (RCurlyTok.isNot(AsmToken::RCurly)) {
713 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000714 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000715 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000716
Bill Wendlinge7176102010-11-06 22:36:58 +0000717 SMLoc E = RCurlyTok.getLoc();
718 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000719
Bill Wendlinge7176102010-11-06 22:36:58 +0000720 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000721 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000722 RI = Registers.begin(), RE = Registers.end();
723
Bill Wendling7caebff2011-01-12 21:20:59 +0000724 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000725 bool EmittedWarning = false;
726
Bill Wendling7caebff2011-01-12 21:20:59 +0000727 DenseMap<unsigned, bool> RegMap;
728 RegMap[HighRegNum] = true;
729
Bill Wendlinge7176102010-11-06 22:36:58 +0000730 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000731 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000732 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000733
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000734 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000735 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000736 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000737 }
738
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000739 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000740 Warning(RegInfo.second,
741 "register not in ascending order in register list");
742
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000743 RegMap[Reg] = true;
744 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000745 }
746
Bill Wendling50d0f582010-11-18 23:43:05 +0000747 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
748 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000749}
750
Bill Wendlinge7176102010-11-06 22:36:58 +0000751/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000752/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000753///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000754/// TODO Only preindexing and postindexing addressing are started, unindexed
755/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000756bool ARMAsmParser::
757ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000758 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000759 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000760 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000761 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000762 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000763
Sean Callanan18b83232010-01-19 21:44:56 +0000764 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000765 if (BaseRegTok.isNot(AsmToken::Identifier)) {
766 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000767 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000768 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000769 int BaseRegNum = TryParseRegister();
770 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000771 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000772 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000773 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000774
Daniel Dunbar05710932011-01-18 05:34:17 +0000775 // The next token must either be a comma or a closing bracket.
776 const AsmToken &Tok = Parser.getTok();
777 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
778 return true;
779
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000780 bool Preindexed = false;
781 bool Postindexed = false;
782 bool OffsetIsReg = false;
783 bool Negative = false;
784 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000785 ARMOperand *WBOp = 0;
786 int OffsetRegNum = -1;
787 bool OffsetRegShifted = false;
788 enum ShiftType ShiftType = Lsl;
789 const MCExpr *ShiftAmount = 0;
790 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000791
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000792 // First look for preindexed address forms, that is after the "[Rn" we now
793 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000794 if (Tok.is(AsmToken::Comma)) {
795 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000796 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000797
Chris Lattner550276e2010-10-28 20:52:15 +0000798 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
799 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000800 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000801 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000802 if (RBracTok.isNot(AsmToken::RBrac)) {
803 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000804 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000805 }
Sean Callanan76264762010-04-02 22:27:05 +0000806 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000807 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000808
Sean Callanan18b83232010-01-19 21:44:56 +0000809 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000810 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000811 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
812 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000813 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000814 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000815 }
Daniel Dunbar05710932011-01-18 05:34:17 +0000816 } else {
817 // The "[Rn" we have so far was not followed by a comma.
818
Jim Grosbach80eb2332010-10-29 17:41:25 +0000819 // If there's anything other than the right brace, this is a post indexing
820 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000821 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000822 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000823
Sean Callanan18b83232010-01-19 21:44:56 +0000824 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000825
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000826 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000827 Postindexed = true;
828 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000829
Chris Lattner550276e2010-10-28 20:52:15 +0000830 if (NextTok.isNot(AsmToken::Comma)) {
831 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000832 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000833 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000834
Sean Callananb9a25b72010-01-19 20:27:46 +0000835 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000836
Chris Lattner550276e2010-10-28 20:52:15 +0000837 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000838 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000839 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000840 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000841 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000842 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000843
844 // Force Offset to exist if used.
845 if (!OffsetIsReg) {
846 if (!Offset)
847 Offset = MCConstantExpr::Create(0, getContext());
848 }
849
850 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
851 OffsetRegNum, OffsetRegShifted,
852 ShiftType, ShiftAmount, Preindexed,
853 Postindexed, Negative, Writeback,
854 S, E));
855 if (WBOp)
856 Operands.push_back(WBOp);
857
858 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000859}
860
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000861/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
862/// we will parse the following (were +/- means that a plus or minus is
863/// optional):
864/// +/-Rm
865/// +/-Rm, shift
866/// #offset
867/// we return false on success or an error otherwise.
868bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000869 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000870 enum ShiftType &ShiftType,
871 const MCExpr *&ShiftAmount,
872 const MCExpr *&Offset,
873 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000874 int &OffsetRegNum,
875 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000876 Negative = false;
877 OffsetRegShifted = false;
878 OffsetIsReg = false;
879 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000880 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000881 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000882 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000883 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000884 else if (NextTok.is(AsmToken::Minus)) {
885 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000886 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000887 }
888 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000889 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000890 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000891 SMLoc CurLoc = OffsetRegTok.getLoc();
892 OffsetRegNum = TryParseRegister();
893 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000894 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000895 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000896 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000897 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000898
Bill Wendling12f40e92010-11-06 10:51:53 +0000899 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000900 if (OffsetRegNum != -1) {
901 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000902 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000903 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000904 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000905
Sean Callanan18b83232010-01-19 21:44:56 +0000906 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000907 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000908 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000909 OffsetRegShifted = true;
910 }
911 }
912 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
913 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000914 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000915 if (HashTok.isNot(AsmToken::Hash))
916 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000917
Sean Callananb9a25b72010-01-19 20:27:46 +0000918 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000919
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000920 if (getParser().ParseExpression(Offset))
921 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000922 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000923 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000924 return false;
925}
926
927/// ParseShift as one of these two:
928/// ( lsl | lsr | asr | ror ) , # shift_amount
929/// rrx
930/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000931bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000932 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000933 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000934 if (Tok.isNot(AsmToken::Identifier))
935 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000936 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000937 if (ShiftName == "lsl" || ShiftName == "LSL")
938 St = Lsl;
939 else if (ShiftName == "lsr" || ShiftName == "LSR")
940 St = Lsr;
941 else if (ShiftName == "asr" || ShiftName == "ASR")
942 St = Asr;
943 else if (ShiftName == "ror" || ShiftName == "ROR")
944 St = Ror;
945 else if (ShiftName == "rrx" || ShiftName == "RRX")
946 St = Rrx;
947 else
948 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000949 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000950
951 // Rrx stands alone.
952 if (St == Rrx)
953 return false;
954
955 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000956 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000957 if (HashTok.isNot(AsmToken::Hash))
958 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000959 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000960
961 if (getParser().ParseExpression(ShiftAmount))
962 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000963
964 return false;
965}
966
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000967/// Parse a arm instruction operand. For now this parses the operand regardless
968/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000969bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000970 bool hasCoprocOp){
Sean Callanan76264762010-04-02 22:27:05 +0000971 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000972 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000973 default:
974 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000975 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +0000976 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +0000977 if (!TryParseRegisterWithWriteBack(Operands))
978 return false;
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +0000979 if (hasCoprocOp && !TryParseCoprocessorOperandName(Operands))
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000980 return false;
981
982 // Fall though for the Identifier case that is not a register or a
983 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +0000984 case AsmToken::Integer: // things like 1f and 2b as a branch targets
985 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +0000986 // This was not a register so parse other operands that start with an
987 // identifier (like labels) as expressions and create them as immediates.
988 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000989 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000990 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000991 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000992 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000993 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
994 return false;
995 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000996 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000997 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000998 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000999 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001000 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001001 // #42 -> immediate.
1002 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001003 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001004 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001005 const MCExpr *ImmVal;
1006 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001007 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001008 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001009 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1010 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001011 case AsmToken::Colon: {
1012 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001013 // FIXME: Check it's an expression prefix,
1014 // e.g. (FOO - :lower16:BAR) isn't legal.
1015 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001016 if (ParsePrefix(RefKind))
1017 return true;
1018
Evan Cheng75972122011-01-13 07:58:56 +00001019 const MCExpr *SubExprVal;
1020 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001021 return true;
1022
Evan Cheng75972122011-01-13 07:58:56 +00001023 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1024 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001025 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001026 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001027 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001028 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001029 }
1030}
1031
Evan Cheng75972122011-01-13 07:58:56 +00001032// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1033// :lower16: and :upper16:.
1034bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1035 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001036
1037 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001038 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001039 Parser.Lex(); // Eat ':'
1040
1041 if (getLexer().isNot(AsmToken::Identifier)) {
1042 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1043 return true;
1044 }
1045
1046 StringRef IDVal = Parser.getTok().getIdentifier();
1047 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001048 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001049 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001050 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001051 } else {
1052 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1053 return true;
1054 }
1055 Parser.Lex();
1056
1057 if (getLexer().isNot(AsmToken::Colon)) {
1058 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1059 return true;
1060 }
1061 Parser.Lex(); // Eat the last ':'
1062 return false;
1063}
1064
1065const MCExpr *
1066ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1067 MCSymbolRefExpr::VariantKind Variant) {
1068 // Recurse over the given expression, rebuilding it to apply the given variant
1069 // to the leftmost symbol.
1070 if (Variant == MCSymbolRefExpr::VK_None)
1071 return E;
1072
1073 switch (E->getKind()) {
1074 case MCExpr::Target:
1075 llvm_unreachable("Can't handle target expr yet");
1076 case MCExpr::Constant:
1077 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1078
1079 case MCExpr::SymbolRef: {
1080 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1081
1082 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1083 return 0;
1084
1085 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1086 }
1087
1088 case MCExpr::Unary:
1089 llvm_unreachable("Can't handle unary expressions yet");
1090
1091 case MCExpr::Binary: {
1092 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1093 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1094 const MCExpr *RHS = BE->getRHS();
1095 if (!LHS)
1096 return 0;
1097
1098 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1099 }
1100 }
1101
1102 assert(0 && "Invalid expression kind!");
1103 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001104}
1105
Daniel Dunbar352e1482011-01-11 15:59:50 +00001106/// \brief Given a mnemonic, split out possible predication code and carry
1107/// setting letters to form a canonical mnemonic and flags.
1108//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001109// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001110static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
1111 unsigned &PredicationCode,
1112 bool &CarrySetting) {
1113 PredicationCode = ARMCC::AL;
1114 CarrySetting = false;
1115
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001116 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001117 //
1118 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001119 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1120 Mnemonic == "movs" ||
1121 Mnemonic == "svc" ||
1122 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1123 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1124 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1125 Mnemonic == "vclt" ||
1126 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1127 Mnemonic == "vcle" ||
1128 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1129 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1130 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001131 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001132
Daniel Dunbar352e1482011-01-11 15:59:50 +00001133 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001134 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001135 .Case("eq", ARMCC::EQ)
1136 .Case("ne", ARMCC::NE)
1137 .Case("hs", ARMCC::HS)
1138 .Case("lo", ARMCC::LO)
1139 .Case("mi", ARMCC::MI)
1140 .Case("pl", ARMCC::PL)
1141 .Case("vs", ARMCC::VS)
1142 .Case("vc", ARMCC::VC)
1143 .Case("hi", ARMCC::HI)
1144 .Case("ls", ARMCC::LS)
1145 .Case("ge", ARMCC::GE)
1146 .Case("lt", ARMCC::LT)
1147 .Case("gt", ARMCC::GT)
1148 .Case("le", ARMCC::LE)
1149 .Case("al", ARMCC::AL)
1150 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001151 if (CC != ~0U) {
1152 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001153 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001154 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001155
Daniel Dunbar352e1482011-01-11 15:59:50 +00001156 // Next, determine if we have a carry setting bit. We explicitly ignore all
1157 // the instructions we know end in 's'.
1158 if (Mnemonic.endswith("s") &&
1159 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1160 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1161 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1162 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1163 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1164 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1165 CarrySetting = true;
1166 }
1167
1168 return Mnemonic;
1169}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001170
1171/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1172/// inclusion of carry set or predication code operands.
1173//
1174// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001175void ARMAsmParser::
1176GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1177 bool &CanAcceptPredicationCode) {
1178 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1179
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001180 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1181 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1182 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1183 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1184 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1185 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1186 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1187 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1188 CanAcceptCarrySet = true;
1189 } else {
1190 CanAcceptCarrySet = false;
1191 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001192
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001193 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1194 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1195 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1196 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001197 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001198 CanAcceptPredicationCode = false;
1199 } else {
1200 CanAcceptPredicationCode = true;
1201 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001202
1203 if (isThumb)
1204 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
1205 Mnemonic == "mrc" || Mnemonic == "mrrc")
1206 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001207}
1208
1209/// Parse an arm instruction mnemonic followed by its operands.
1210bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1211 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1212 // Create the leading tokens for the mnemonic, split by '.' characters.
1213 size_t Start = 0, Next = Name.find('.');
1214 StringRef Head = Name.slice(Start, Next);
1215
Daniel Dunbar352e1482011-01-11 15:59:50 +00001216 // Split out the predication code and carry setting flag from the mnemonic.
1217 unsigned PredicationCode;
1218 bool CarrySetting;
1219 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001220
Chris Lattner3a697562010-10-28 17:20:03 +00001221 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001222
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001223 // Next, add the CCOut and ConditionCode operands, if needed.
1224 //
1225 // For mnemonics which can ever incorporate a carry setting bit or predication
1226 // code, our matching model involves us always generating CCOut and
1227 // ConditionCode operands to match the mnemonic "as written" and then we let
1228 // the matcher deal with finding the right instruction or generating an
1229 // appropriate error.
1230 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1231 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1232
1233 // Add the carry setting operand, if necessary.
1234 //
1235 // FIXME: It would be awesome if we could somehow invent a location such that
1236 // match errors on this operand would print a nice diagnostic about how the
1237 // 's' character in the mnemonic resulted in a CCOut operand.
1238 if (CanAcceptCarrySet) {
1239 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1240 NameLoc));
1241 } else {
1242 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1243 // misspelled another mnemonic).
1244
1245 // FIXME: Issue a nice error.
1246 }
1247
1248 // Add the predication code operand, if necessary.
1249 if (CanAcceptPredicationCode) {
1250 Operands.push_back(ARMOperand::CreateCondCode(
1251 ARMCC::CondCodes(PredicationCode), NameLoc));
1252 } else {
1253 // This mnemonic can't ever accept a predication code, but the user wrote
1254 // one (or misspelled another mnemonic).
1255
1256 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001257 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001258
1259 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001260 while (Next != StringRef::npos) {
1261 Start = Next;
1262 Next = Name.find('.', Start + 1);
1263 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001264
Chris Lattner3a697562010-10-28 17:20:03 +00001265 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001266 }
1267
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +00001268 // Enable the parsing of instructions containing coprocessor related
1269 // asm syntax, such as coprocessor names "p7, p15, ..." and coprocessor
1270 // registers "c1, c3, ..."
1271 // FIXME: we probably want AsmOperandClass and ParserMatchClass declarations
1272 // in the .td file rather than hacking the ASMParser for every symbolic
1273 // operand type.
1274 bool hasCoprocOp = (Head == "mcr" || Head == "mcr2" ||
1275 Head == "mcrr" || Head == "mcrr2" ||
1276 Head == "mrc" || Head == "mrc2" ||
1277 Head == "mrrc" || Head == "mrrc2" ||
1278 Head == "cdp" || Head == "cdp2");
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001279
Daniel Dunbar5747b132010-08-11 06:37:16 +00001280 // Read the remaining operands.
1281 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001282 // Read the first operand.
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +00001283 if (ParseOperand(Operands, hasCoprocOp)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001284 Parser.EatToEndOfStatement();
1285 return true;
1286 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001287
1288 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001289 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001290
1291 // Parse and remember the operand.
Bruno Cardoso Lopesb32f7a52011-01-20 18:06:58 +00001292 if (ParseOperand(Operands, hasCoprocOp)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001293 Parser.EatToEndOfStatement();
1294 return true;
1295 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001296 }
1297 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001298
Chris Lattnercbf8a982010-09-11 16:18:25 +00001299 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1300 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001301 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001302 }
Bill Wendling146018f2010-11-06 21:42:12 +00001303
Chris Lattner34e53142010-09-08 05:10:46 +00001304 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001305 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001306}
1307
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001308bool ARMAsmParser::
1309MatchAndEmitInstruction(SMLoc IDLoc,
1310 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1311 MCStreamer &Out) {
1312 MCInst Inst;
1313 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001314 MatchResultTy MatchResult, MatchResult2;
1315 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1316 if (MatchResult != Match_Success) {
1317 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1318 // that does not update the condition codes. So try adding a CCOut operand
1319 // with a value of reg0.
1320 if (MatchResult == Match_InvalidOperand) {
1321 Operands.insert(Operands.begin() + 1,
1322 ARMOperand::CreateCCOut(0,
1323 ((ARMOperand*)Operands[0])->getStartLoc()));
1324 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1325 if (MatchResult2 == Match_Success)
1326 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001327 else {
1328 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001329 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001330 delete CCOut;
1331 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001332 }
1333 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1334 // that updates the condition codes if it ends in 's'. So see if the
1335 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1336 // operand with a value of CPSR.
1337 else if(MatchResult == Match_MnemonicFail) {
1338 // Get the instruction mnemonic, which is the first token.
1339 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1340 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1341 // removed the 's' from the mnemonic for matching.
1342 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1343 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001344 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1345 Operands.erase(Operands.begin());
1346 delete OldMnemonic;
1347 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001348 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1349 Operands.insert(Operands.begin() + 1,
1350 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1351 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1352 if (MatchResult2 == Match_Success)
1353 MatchResult = Match_Success;
1354 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001355 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1356 Operands.erase(Operands.begin());
1357 delete OldMnemonic;
1358 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001359 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001360 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1361 Operands.erase(Operands.begin() + 1);
1362 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001363 }
1364 }
1365 }
1366 }
1367 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001368 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001369 Out.EmitInstruction(Inst);
1370 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001371 case Match_MissingFeature:
1372 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1373 return true;
1374 case Match_InvalidOperand: {
1375 SMLoc ErrorLoc = IDLoc;
1376 if (ErrorInfo != ~0U) {
1377 if (ErrorInfo >= Operands.size())
1378 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001379
Chris Lattnere73d4f82010-10-28 21:41:58 +00001380 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1381 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1382 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001383
Chris Lattnere73d4f82010-10-28 21:41:58 +00001384 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001385 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001386 case Match_MnemonicFail:
1387 return Error(IDLoc, "unrecognized instruction mnemonic");
1388 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001389
Eric Christopherc223e2b2010-10-29 09:26:59 +00001390 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001391 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001392}
1393
Kevin Enderby515d5092009-10-15 20:48:48 +00001394/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001395bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1396 StringRef IDVal = DirectiveID.getIdentifier();
1397 if (IDVal == ".word")
1398 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001399 else if (IDVal == ".thumb")
1400 return ParseDirectiveThumb(DirectiveID.getLoc());
1401 else if (IDVal == ".thumb_func")
1402 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1403 else if (IDVal == ".code")
1404 return ParseDirectiveCode(DirectiveID.getLoc());
1405 else if (IDVal == ".syntax")
1406 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001407 return true;
1408}
1409
1410/// ParseDirectiveWord
1411/// ::= .word [ expression (, expression)* ]
1412bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1413 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1414 for (;;) {
1415 const MCExpr *Value;
1416 if (getParser().ParseExpression(Value))
1417 return true;
1418
Chris Lattneraaec2052010-01-19 19:46:13 +00001419 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001420
1421 if (getLexer().is(AsmToken::EndOfStatement))
1422 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001423
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001424 // FIXME: Improve diagnostic.
1425 if (getLexer().isNot(AsmToken::Comma))
1426 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001427 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001428 }
1429 }
1430
Sean Callananb9a25b72010-01-19 20:27:46 +00001431 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001432 return false;
1433}
1434
Kevin Enderby515d5092009-10-15 20:48:48 +00001435/// ParseDirectiveThumb
1436/// ::= .thumb
1437bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1438 if (getLexer().isNot(AsmToken::EndOfStatement))
1439 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001440 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001441
1442 // TODO: set thumb mode
1443 // TODO: tell the MC streamer the mode
1444 // getParser().getStreamer().Emit???();
1445 return false;
1446}
1447
1448/// ParseDirectiveThumbFunc
1449/// ::= .thumbfunc symbol_name
1450bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001451 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001452 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001453 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001454 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001455 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001456 if (getLexer().isNot(AsmToken::EndOfStatement))
1457 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001458 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001459
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001460 // Mark symbol as a thumb symbol.
1461 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1462 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001463 return false;
1464}
1465
1466/// ParseDirectiveSyntax
1467/// ::= .syntax unified | divided
1468bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001469 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001470 if (Tok.isNot(AsmToken::Identifier))
1471 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001472 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001473 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001474 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001475 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001476 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001477 else
1478 return Error(L, "unrecognized syntax mode in .syntax directive");
1479
1480 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001481 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001482 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001483
1484 // TODO tell the MC streamer the mode
1485 // getParser().getStreamer().Emit???();
1486 return false;
1487}
1488
1489/// ParseDirectiveCode
1490/// ::= .code 16 | 32
1491bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001492 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001493 if (Tok.isNot(AsmToken::Integer))
1494 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001495 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001496 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001497 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001498 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001499 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001500 else
1501 return Error(L, "invalid operand to .code directive");
1502
1503 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001504 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001505 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001506
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001507 // FIXME: We need to be able switch subtargets at this point so that
1508 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1509 // includes Feature_IsThumb or not to match the right instructions. This is
1510 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1511 if (Val == 16){
1512 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1513 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001514 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001515 }
1516 else{
1517 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1518 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001519 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001520 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001521
Kevin Enderby515d5092009-10-15 20:48:48 +00001522 return false;
1523}
1524
Sean Callanan90b70972010-04-07 20:29:34 +00001525extern "C" void LLVMInitializeARMAsmLexer();
1526
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001527/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001528extern "C" void LLVMInitializeARMAsmParser() {
1529 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1530 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001531 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001532}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001533
Chris Lattner0692ee62010-09-06 19:11:01 +00001534#define GET_REGISTER_MATCHER
1535#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001536#include "ARMGenAsmMatcher.inc"